Assignment
Numeric Assignment
Scalar quantum variables (qnum and qbit) can be assigned the result of arithmetic/logical
expressions
Syntax
- Python
- Native
target-var |= quantum-expression
OR
assign(quantum-expression, target-var**)**target-var ^= quantum-expression
OR
inplace_xor(quantum-expression, target-var**)**target-var += quantum-expression
OR
inplace_add(quantum-expression, target-var**)**
OR
assign(quantum-expression, target-var**)**target-var ^= quantum-expression
OR
inplace_xor(quantum-expression, target-var**)**target-var += quantum-expression
OR
inplace_add(quantum-expression, target-var**)**
Notes
- The operator
|=is used to represent the native=since the operator=cannot be overloaded in Python. - The operator syntax and the function call syntax are equivalent. The operator syntax is typically easier to read, but it cannot be used directly in lambda expressions, where the function call syntax should be used.
Semantics
- quantum-expression consists of quantum scalar variables, numeric constant literals, and classical scalar variables, composed using arithmetic operators. See below the set of supported operators.
- The quantum variables occurring in the expression can subsequently be used, with their states unmodified.
Out-of-place assignment (=/|=)
- target-var must be uninitialized prior to the assignment and is subsequently initialized.
- The size and numeric attributes of target-var are computed to tightly fit the range of possible result values of quantum-expression, based on variable sizes, constants, and operators.
- The numeric attributes of target-var must be left unspecified in the declaration or otherwise be compatible with the computed numeric attributes of quantum-expression, that is, fit the entire range of possible expression values.
In-place XOR (^=)
- target-var must be initialized prior to the assignment.
- Each bit in target-var is xor-ed with the respective bit in the result of quantum-expression if any, or otherwise left unchanged.
- Bits in the result of quantum-expression with no counterpart in target-var are ignored.
In-place add (+=)
- target-var must be initialized prior to the assignment.
- The result of quantum-expression is added to the numeric value of target-var according to the
two's complement
- Superfluous fraction digits in quantum-expression are ignored. Superfluous fraction digits in target-var remain untouched.
- When target-var overflows or underflows, its value is wrapped-around the integer part (including the sign bit) without incurring additional qubits, following the two’s complement method.
Aggregate Type Assignment
A struct or array quantum variable can be assigned to another variable of the same type. In addition, an array literal can be assigned to aQArray[QBit] variable.
Syntax
- Python
- Native
target-var |= assigned-var
OR
assign(assigned-var, target-var**)**target-var ^= assigned-var
OR
inplace_xor(assigned-var, target-var**)**array-var |= array-literal
OR
assign(array-literal, array-var**)**target-var ^= array-literal
OR
inplace_xor(array-literal, array-var**)**
OR
assign(assigned-var, target-var**)**target-var ^= assigned-var
OR
inplace_xor(assigned-var, target-var**)**array-var |= array-literal
OR
assign(array-literal, array-var**)**target-var ^= array-literal
OR
inplace_xor(array-literal, array-var**)**
Notes
- The operator
|=is used to represent the native=since the operator=cannot be overloaded in Python. - The operator syntax and the function call syntax are equivalent. The operator syntax is typically easier to read, but it cannot be used directly in lambda expressions, where the function call syntax should be used.
Semantics
- As with numeric assignments, target-var must be uninitialized in out-of-place assignments and initialized in in-place assignments. assigned-var must be initialized in both cases.
- target-var and assigned-var must have the same type. For example,
variable of type
QArray[QBit]can be assigned into aQArray[QBit]variable, but aQArray[QNum]variable cannot. - array-literal is a classical array of 0-s and 1-s. array-var must be
a quantum variable of type
QArray[QBit].
Examples
Example 1: Out-of-place assignment
The following is a model that computes the result of the expressiona + 2 * b + 3,
with a initialized to 3 and b initialized to a superposition of 1 and 2.
The output is a superposition of 8 and 10.
- Python
- Native
a and b are two-qubit variables. Any other size declared for res
will result in an error.
Example 2: In-place XOR assignment
In the next example, the relational expressiona + 2 * b + 3 == 8 is computed, with
a initialized to 3 and b initialized to 1. Calling function foo will flip the
single variable res, because the expression evaluates to 1, that is, true.
- Python
- Native
Example 3: In-place assignment of a logical expression
In the example below, functionmy_oracle serves as a quantum oracle that marks all states
satisfying the logical expression (x0 and x1) or (x2 and x3) with a minus phase.
- Python
- Native
^= is factored out to an inner Python function.Example 4: In-place add assignment
The following model initializes two quantum numeric variablesn and m.
- Python
- Native
m has three qubits, of which one is a sign qubit and two are fraction
digits.
By applying X (not) to m’s qubit, we set its value to -0.25.
When adding m to n (n += m), the variables do not align since variable n
has one less fraction digit than m:
m:
m to the size of n (3) by duplicating the sign bit in
accordance with the two’s complement method:
m to n sets n’s state to 111, whose interpretation is
the numeric value -0.5.
Example 5: Overflowing in-place add assignment
The following model demonstrates what happens to the target variable when its value overflows, i.e., extends beyond the variable domain.- Python
- Native
Example 6: Quantum subscript expression
The following model demonstrate quantum subscript expression over a classical list[7, 3, 6, 2] and a quantum variable index.
The quantum index is in superposition over the indices 0 (10%), 1 (20%), 2
(30%), and 3 (40%).
The quantum subscript expression [7, 3, 6, 2][index] is in superposition over the
items [7, 3, 6, 2] entangled to index.
Overall, the output variable n evaluates to 7 (10%), 3 (20%), 6 (30%), or 2
(40%).
- Python
- Native
Example 7: Aggregate type assignments
The following model demonstrate different kinds of quantum array assignments. First, you assign the array literal[0, 1, 1, 0] into variable qarr1 of type
QArray[QBit].
This applies X to the second and third bits of the array.
Next, you initialize qarr2 by assigning qarr1 to it.
This applies CX to the qubits of qarr1 and qarr2 sequentially.
- Python
- Native