qcircuits.operators module

The operators module contains the Operator class, instances of which represent unitary operators on vector spaces for multi-qubit systems, and factory functions for creating specific operators.

The Operator class inherits from the OperatorBase class, which contains functionality common to unitary operators and the DensityOperator class.

The Operator class and the factory functions used to create operators are aliased at the top-level module, so that, for example, one can call qcircuits.Hadamard() instead of qcircuits.state.Hadamard().

qcircuits.operators.CNOT()

Produce the two-qubit CNOT operator, which flips the second bit if the first bit is set. Maps |00⟩ -> |00⟩, |01⟩ -> |01⟩, |10⟩ -> |01⟩, |11⟩ -> |10⟩.

Returns:A rank 4 tensor describing the operator.
Return type:Operator
qcircuits.operators.ControlledU(U)

Produce a Controlled-U operator, an operator for a d + 1 qubit system where the supplied U is an operator for a d qubit system. If the first bit is set, apply U to the state for the remaining bits.

Parameters:U (Operator) – The operator to be conditionally applied.
Returns:A tensor whose rank is the rank of U plus 2, describing the operator.
Return type:Operator
qcircuits.operators.Hadamard(d=1)

Produce the d-qubit Hadamard operator \(H^{\otimes d}\). Maps: |0⟩ -> \(\frac{1}{\sqrt{2}}\) (|0⟩+|1⟩) = |+⟩, |1⟩ -> \(\frac{1}{\sqrt{2}}\) (|0⟩-|1⟩) = |-⟩.

Parameters:d (int) – The number of qubits described by the state vector on which the produced operator will act.
Returns:A rank 2d tensor describing the operator.
Return type:Operator
qcircuits.operators.Identity(d=1)

Produce the d-qubit identity operator \(I^{\otimes d}\).

Parameters:d (int) – The number of qubits described by the state vector on which the produced operator will act.
Returns:A rank 2d tensor describing the operator.
Return type:Operator
class qcircuits.operators.Operator(tensor)

Bases: qcircuits.operators.OperatorBase

A container class for a tensor representing an operator on a vector space for a quantum system, and associated methods.

Parameters:tensor (numpy complex128 multidimensional array) – The tensor representing the operator.
__call__(arg, qubit_indices=None)

Applies this Operator to another Operator, as in operator composition A(B), or to a State or DensityOperator, as in A(v). Via operator composition, if two operators A and B will be applied to state v in sequence, either B(A(v)) or (B(A))(v) are valid.

A d-qubit operator may be applied to an n-qubit system with \(n>d\) if the qubits to which it is to be applied are specified in the qubit_indices parameter.

If x represents state \(|\phi⟩\) and A an operator, A(x) represents the state \(A |\phi⟩\). If x represents density operator (mixed state) \(\rho\), then A(x) represents the state \(A \rho A^{\dagger}.\)

Parameters:
  • arg (State, Operator, or DensityOperator) – The state that the operator is applied to, or the operator with which the operator is composed.
  • qubit_indices (list of int) – If the operator is applied to a larger quantum system, the user must supply a list of the indices of the qubits to which the operator is to be applied. These can also be used to apply the operator to the qubits in arbitrary order.
Returns:

The state vector or operator resulting in applying the operator to the argument.

Return type:

State, Operator, or DensityOperator

class qcircuits.operators.OperatorBase(tensor)

Bases: qcircuits.tensors.Tensor

A base class for operators and density operators.

Parameters:tensor (numpy complex128 multidimensional array) – The tensor representing the operator.
adj

Get the adjoint of this operator, \(A^{\dagger} = (A^{*})^{T}\). If the operator is unitary, \(A A^{\dagger} = I\).

Returns:The adjoint operator.
Return type:Operator
static from_matrix(M)

QCircuits represents operators for d-qubit systems with type (d, d) tensors. This function constructs an operator from the more common matrix Kronecker-product representation of the operator.

Parameters:complex128 multidimensional array (numpy) – The matrix representation of the operator.
Returns:A d-qubit operator.
Return type:Operator
permute_qubits(axes, inverse=False)

Permute the qubits (i.e., both the incoming and outgoing wires) of the operator. This method modifies the operator in-place, but also returns the resulting operator to allow chaining of operations.

Parameters:
  • axes (list of int) – Permute the qubits according to the values given.
  • inverse (bool) – If true, perform the inverse permutation of the qubits.
Returns:

The resulting operator.

Return type:

Operator

swap_qubits(axis1, axis2)

Swap two qubits (i.e., both the incoming and outgoing wires) of the operator. This method modifies the operator in-place, but also returns the resulting operator to allow chaining of operations.

Parameters:
  • axis1 (int) – First axis.
  • axis2 (int) – Second axis.
Returns:

The resulting operator.

Return type:

Operator

to_matrix()

QCircuits represents operators for d-qubit systems with type (d, d) tensors. This function returns the more common matrix Kronecker-product representation of the operator.

Returns:The matrix representation of the operator.
Return type:numpy complex128 multidimensional array
qcircuits.operators.PauliX(d=1)

Produce the d-qubit Pauli X operator \(X^{\otimes d}\), or not gate. Maps: |0⟩ -> |1⟩, |1⟩ -> |0⟩.

Parameters:d (int) – The number of qubits described by the state vector on which the produced operator will act.
Returns:A rank 2d tensor describing the operator.
Return type:Operator
qcircuits.operators.PauliY(d=1)

Produce the d-qubit Pauli Y operator \(Y^{\otimes d}\). Maps: |0⟩ -> i |1⟩, |1⟩ -> -i |0⟩.

Parameters:d (int) – The number of qubits described by the state vector on which the produced operator will act.
Returns:A rank 2d tensor describing the operator.
Return type:Operator
qcircuits.operators.PauliZ(d=1)

Produce the d-qubit Pauli Z operator \(Z^{\otimes d}\), or phase inverter. Maps: |0⟩ -> |0⟩, |1⟩ -> -|1⟩.

Parameters:d (int) – The number of qubits described by the state vector on which the produced operator will act.
Returns:A rank 2d tensor describing the operator.
Return type:Operator
qcircuits.operators.Phase(d=1)

Produce the d-qubit Phase operator S. Maps: |0⟩ -> |0⟩, |1⟩ -> i|1⟩. Note that \(S^2 = Z\).

Parameters:d (int) – The number of qubits described by the state vector on which the produced operator will act.
Returns:A rank 2d tensor describing the operator.
Return type:Operator
qcircuits.operators.PiBy8(d=1)

Produce the d-qubit \(\pi/8\) operator T. Maps: |0⟩ -> |0⟩, |1⟩ -> \(e^{i\pi/4}\) |1⟩. Note that \(T^2 = S\), where S is the phase gate, and \(T^4 = Z\).

Parameters:d (int) – The number of qubits described by the state vector on which the produced operator will act.
Returns:A rank 2d tensor describing the operator.
Return type:Operator
qcircuits.operators.Rotation(v, theta)

Produce the single-qubit rotation operator. In terms of the Bloch sphere picture of the qubit state, the operator rotates a state through angle :math:` heta` around vector v.

Parameters:
  • v (list of float) – A real 3D unit vector around which the qubit’s Bloch vector is to be rotated.
  • theta (float) – The angle through which the qubit’s Bloch vector is to be rotated.
Returns:

A rank 2 tensor describing the operator.

Return type:

Operator

qcircuits.operators.RotationX(theta)

Produce the single-qubit X-rotation operator. In terms of the Bloch sphere picture of the qubit state, the operator rotates a state through angle theta around the x-axis.

Parameters:theta (float) – The angle through which the qubit’s Bloch vector is to be rotated.
Returns:A rank 2 tensor describing the operator.
Return type:Operator
qcircuits.operators.RotationY(theta)

Produce the single-qubit Y-rotation operator. In terms of the Bloch sphere picture of the qubit state, the operator rotates a state through angle theta around the y-axis.

Parameters:theta (float) – The angle through which the qubit’s Bloch vector is to be rotated.
Returns:A rank 2 tensor describing the operator.
Return type:Operator
qcircuits.operators.RotationZ(theta)

Produce the single-qubit Z-rotation operator. In terms of the Bloch sphere picture of the qubit state, the operator rotates a state through angle theta around the z-axis.

Parameters:theta (float) – The angle through which the qubit’s Bloch vector is to be rotated.
Returns:A rank 2 tensor describing the operator.
Return type:Operator
qcircuits.operators.SqrtNot(d=1)

Produce the d-qubit operator that is the square root of the d-qubit NOT or PauliX() operator, i.e., \(\sqrt{\texttt{NOT}}(\sqrt{\texttt{NOT}}) = X\).

Parameters:d (int) – The number of qubits described by the state vector on which the produced operator will act.
Returns:A rank 2d tensor describing the operator.
Return type:Operator
qcircuits.operators.SqrtSwap()

Produce the two-qubit operator that is the square root of the Swap() operator, i.e., \(\sqrt{\texttt{SWAP}}(\sqrt{\texttt{SWAP}}) = \texttt{SWAP}\).

Returns:A rank 4 tensor describing the operator.
Return type:Operator
qcircuits.operators.Swap()

Produce the two-qubit SWAP operator, which swaps two bits. Maps |00⟩ -> |00⟩, |01⟩ -> |10⟩, |10⟩ -> |01⟩, |11⟩ -> |11⟩.

Returns:A rank 4 tensor describing the operator.
Return type:Operator
qcircuits.operators.Toffoli()

Produce the three-qubit Toffoli operator, which flips the third bit if the first two bits are set. Maps |110⟩ -> |111⟩, |111⟩ -> |110⟩, and otherwise acts as the identity.

Returns:A rank 6 tensor describing the operator.
Return type:Operator
qcircuits.operators.U_f(f, d)

Produce a U_f operator, an operator for a d qubit system that flips the last bit based on the outcome of a supplied boolean function \(f: [0, 1]^{d-1} \to [0, 1]\) applied to the first d - 1 bits.

Parameters:f (function) – The boolean function used to conditionally flip the last bit.
Returns:A rank 2d tensor describing the operator.
Return type:Operator