matrix_expander
can1
Generates the unitary matrix for a canonical single-qubit gate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
axis
|
AxisLike
|
The rotation axis. |
required |
angle
|
float
|
The rotation angle in radians. |
required |
phase
|
float
|
The phase in radians. |
0
|
Returns:
| Type | Description |
|---|---|
NDArray[complex128]
|
The unitary \(2\times 2\) matrix. |
Source code in opensquirrel/utils/matrix_expander.py
can2
Generates the unitary matrix for a canonical two-qubit gate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
canonical_axis
|
AxisLike
|
The canonical axis. |
required |
Returns:
| Type | Description |
|---|---|
NDArray[complex128]
|
The unitary \(4\times 4\) matrix. |
Source code in opensquirrel/utils/matrix_expander.py
expand_ket
Given a base quantum ket on \(n\) qubits and a reduced ket on a subset of those qubits, this computes the expanded ket where the reduction qubits and the other qubits are set based on the reduced ket and the base ket, respectively.
Roughly equivalent to the PDEP assembly instruction (bits deposit).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_ket
|
int
|
A quantum ket, represented by its corresponding non-negative integer. By convention, qubit #0 corresponds to the least significant bit. |
required |
reduced_ket
|
int
|
A quantum ket, represented by its corresponding non-negative integer. By convention, qubit #0 corresponds to the least significant bit. |
required |
qubits
|
Iterable[QubitLike]
|
The indices of the qubits to expand from the reduced ket. Order matters. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The non-negative integer corresponding to the expanded ket. |
Example
expand_ket(0b00000, 0b0, [Qubit(5)]) # 0b000000 0 expand_ket(0b00000, 0b1, [Qubit(5)]) # 0b100000 32 expand_ket(0b00111, 0b0, [Qubit(5)]) # 0b000111 7 expand_ket(0b00111, 0b1, [Qubit(5)]) # 0b100111 39 expand_ket(0b0000, 0b000, [Qubit(1), Qubit(2), Qubit(3)]) # 0b0000 0 expand_ket(0b0000, 0b001, [Qubit(1), Qubit(2), Qubit(3)]) # 0b0010 2 expand_ket(0b0000, 0b011, [Qubit(1), Qubit(2), Qubit(3)]) # 0b0110 6 expand_ket(0b0000, 0b101, [Qubit(1), Qubit(2), Qubit(3)]) # 0b1010 10 expand_ket(0b0001, 0b101, [Qubit(1), Qubit(2), Qubit(3)]) # 0b1011 11
Source code in opensquirrel/utils/matrix_expander.py
get_matrix
Compute the unitary matrix corresponding to the gate applied to those qubit operands, taken among any number of qubits. This can be used for, e.g.,
- testing,
- permuting the operands of multi-qubit gates,
- simulating a circuit (simulation in this way is inefficient for large numbers of qubits).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gate
|
Gate
|
The gate, including the qubits on which it is operated on. |
required |
qubit_register_size
|
int
|
The size of the qubit register. |
required |
Returns:
| Type | Description |
|---|---|
NDArray[complex128]
|
The unitary matrix corresponding to the gate applied to the qubit operands. |
Example
X = lambda q: BlochSphereRotation(qubit=q, axis=(1, 0, 0), angle=math.pi, phase=math.pi / 2) get_matrix(X(1), 2).astype(int) # X q[1] array([[0, 0, 1, 0], [0, 0, 0, 1], [1, 0, 0, 0], [0, 1, 0, 0]]) CNOT02 = ControlledGate(0, X(2)) get_matrix(CNOT02, 3).astype(int) # CNOT q[0], q[2] array([[1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 1, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 1, 0, 0, 0, 0]]) get_matrix(ControlledGate(1, X(2)), 3).astype(int) # CNOT q[1], q[2] array([[1, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0]])
Source code in opensquirrel/utils/matrix_expander.py
get_reduced_ket
Given a quantum ket represented by its corresponding base-10 integer, this computes the reduced ket where only the given qubits appear, in order.
Roughly equivalent to the PEXT assembly instruction (bits extraction).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ket
|
int
|
A quantum ket, represented by its corresponding non-negative integer. By convention, qubit #0 corresponds to the least significant bit. |
required |
qubits
|
Iterable[QubitLike]
|
The indices of the qubits to extract. Order matters. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The non-negative integer corresponding to the reduced ket. |
Example
get_reduced_ket(1, [Qubit(0)]) # 0b01 1 get_reduced_ket(1111, [Qubit(2)]) # 0b01 1 get_reduced_ket(1111, [Qubit(5)]) # 0b0 0 get_reduced_ket(1111, [Qubit(2), Qubit(5)]) # 0b01 1 get_reduced_ket(101, [Qubit(1), Qubit(0)]) # 0b10 2 get_reduced_ket(101, [Qubit(0), Qubit(1)]) # 0b01 1