Gates

Gate objects

class quantumflow.Gate(tensor: TensorLike, qubits: Qubits = None, params: Dict[str, float] = None, name: str = None)

A quantum logic gate. An operator that acts upon a collection of qubits.

Variables:
  • params (dict) – Optional keyword parameters used to create this gate
  • name (str) – The name of this gate
H

Return the Hermitian conjugate of this quantum operation.

For unitary Gates (and Circuits composed of the same) the Hermitian conjugate returns the inverse Gate (or Circuit)

aschannel() → qf.Channel

Converts a Gate into a Channel

asgate() → qf.Gate

Convert this quantum operation to a gate (if possible)

asoperator() → BKTensor

Return the gate tensor as a square array

evolve(rho: qf.Density) → qf.Density

Apply the action of this gate upon a density

name

Return the name of this operation

permute(qubits: Qubits) → qf.Gate

Permute the order of the qubits

qubit_nb

Return the total number of qubits

qubits

Return the qubits that this operation acts upon

relabel(qubits: Qubits) → qf.Gate

Return a copy of this Gate with new qubits

run(ket: qf.State) → qf.State

Apply the action of this gate upon a state

tensor

Returns the tensor representation of gate operator

Actions on gates

quantumflow.join_gates(*gates) → qf.Gate

Direct product of two gates. Qubit count is the sum of each gate’s bit count.

quantumflow.control_gate(control: Qubit, gate: qf.Gate) → qf.Gate

Return a controlled unitary gate. Given a gate acting on K qubits, return a new gate on K+1 qubits prepended with a control bit.

quantumflow.conditional_gate(control: Qubit, gate0: qf.Gate, gate1: qf.Gate) → qf.Gate

Return a conditional unitary gate. Do gate0 on bit 1 if bit 0 is zero, else do gate1 on 1

quantumflow.almost_unitary(gate: qf.Gate) → bool

Return true if gate tensor is (almost) unitary

quantumflow.print_gate(gate: qf.Gate, ndigits: int = 2, file: TextIO = None) → None

Pretty print a gate tensor

Parameters:
  • gate
  • ndigits
  • file – Stream to which to write. Defaults to stdout

Standard gates

Standard gate set, as detailed in Quil whitepaper (arXiv:1608:03355v2)

Standard one-qubit gates

class quantumflow.I(*qubits)

The identity gate.

This gate can take a variable number of qubits.

\[\begin{split}I() \equiv \begin{pmatrix} 1 & 0 \\ 0 & 1 \end{pmatrix}\end{split}\]
class quantumflow.X(q0: Qubit = 0)

A 1-qubit Pauli-X gate.

\[\begin{split}X() &\equiv \begin{pmatrix} 0 & 1 \\ 1 & 0 \end{pmatrix}\end{split}\]
class quantumflow.Y(q0: Qubit = 0)

A 1-qubit Pauli-Y gate.

\[\begin{split}Y() &\equiv \begin{pmatrix} 0 & -i \\ i & 0 \end{pmatrix}\end{split}\]

mnemonic: “Minus eye high”.

class quantumflow.Z(q0: Qubit = 0)

A 1-qubit Pauli-Z gate.

\[\begin{split}Z() &\equiv \begin{pmatrix} 1 & 0 \\ 0 & -1 \end{pmatrix}\end{split}\]
class quantumflow.H(q0: Qubit = 0)

A 1-qubit Hadamard gate.

\[\begin{split}H() \equiv \frac{1}{\sqrt{2}} \begin{pmatrix} 1 & 1 \\ 1 & -1 \end{pmatrix}\end{split}\]
class quantumflow.S(q0: Qubit = 0)

A 1-qubit phase S gate, equivalent to PHASE(pi/2). The square root of the Z gate (up to global phase). Also commonly denoted as the P gate.

\[\begin{split}S() \equiv \begin{pmatrix} 1 & 0 \\ 0 & i \end{pmatrix}\end{split}\]
class quantumflow.T(q0: Qubit = 0)

A 1-qubit T (pi/8) gate, equivalent to PHASE(pi/4). The forth root of the Z gate (up to global phase).

\[\begin{split}\begin{pmatrix} 1 & 0 \\ 0 & e^{i \pi / 4} \end{pmatrix}\end{split}\]
class quantumflow.PHASE(theta: float, q0: Qubit = 0)

A 1-qubit parametric phase shift gate

\[\begin{split}\text{PHASE}(\theta) \equiv \begin{pmatrix} 1 & 0 \\ 0 & e^{i \theta} \end{pmatrix}\end{split}\]
class quantumflow.RX(theta: float, q0: Qubit = 0)

A 1-qubit Pauli-X parametric rotation gate.

\[\begin{split}R_X(\theta) = \begin{pmatrix} \cos(\frac{\theta}{2}) & -i \sin(\theta/2) \\ -i \sin(\theta/2) & \cos(\theta/2) \end{pmatrix}\end{split}\]
Parameters:theta – Angle of rotation in Bloch sphere
class quantumflow.RY(theta: float, q0: Qubit = 0)

A 1-qubit Pauli-Y parametric rotation gate

\[\begin{split}R_Y(\theta) \equiv \begin{pmatrix} \cos(\theta / 2) & -\sin(\theta / 2) \\ \sin(\theta/2) & \cos(\theta/2) \end{pmatrix}\end{split}\]
Parameters:theta – Angle of rotation in Bloch sphere
class quantumflow.RZ(theta: float, q0: Qubit = 0)

A 1-qubit Pauli-X parametric rotation gate

\[\begin{split}R_Z(\theta)\equiv \begin{pmatrix} \cos(\theta/2) - i \sin(\theta/2) & 0 \\ 0 & \cos(\theta/2) + i \sin(\theta/2) \end{pmatrix}\end{split}\]
Parameters:theta – Angle of rotation in Bloch sphere

Standard two-qubit gates

class quantumflow.CZ(q0: Qubit = 0, q1: Qubit = 1)

A controlled-Z gate

Equivalent to controlled_gate(Z()) and locally equivalent to CANONICAL(1/2,0,0)

\[\begin{split}\text{CZ}() = \begin{pmatrix} 1&0&0&0 \\ 0&1&0&0 \\ 0&0&1&0 \\ 0&0&0&-1 \end{pmatrix}\end{split}\]
class quantumflow.CNOT(q0: Qubit = 0, q1: Qubit = 1)

A controlled-not gate

Equivalent to controlled_gate(X()), and locally equivalent to CANONICAL(1/2, 0, 0)

\[\begin{split}\text{CNOT}() \equiv \begin{pmatrix} 1&0&0&0 \\ 0&1&0&0 \\ 0&0&0&1 \\ 0&0&1&0 \end{pmatrix}\end{split}\]
class quantumflow.SWAP(q0: Qubit = 0, q1: Qubit = 1)

A 2-qubit swap gate

Locally equivalent to CANONICAL(1/2,1/2,1/2).

\[\begin{split}\text{SWAP}() \equiv \begin{pmatrix} 1&0&0&0 \\ 0&0&1&0 \\ 0&1&0&0 \\ 0&0&0&1 \end{pmatrix}\end{split}\]
class quantumflow.ISWAP(q0: Qubit = 0, q1: Qubit = 1)

A 2-qubit iswap gate

Locally equivalent to CANONICAL(1/2,1/2,0).

\[\begin{split}\text{ISWAP}() \equiv \begin{pmatrix} 1&0&0&0 \\ 0&0&i&0 \\ 0&i&0&0 \\ 0&0&0&1 \end{pmatrix}\end{split}\]
class quantumflow.CPHASE00(theta: float, q0: Qubit = 0, q1: Qubit = 1)

A 2-qubit 00 phase-shift gate

\[\text{CPHASE00}(\theta) \equiv \text{diag}(e^{i \theta}, 1, 1, 1)\]
class quantumflow.CPHASE01(theta: float, q0: Qubit = 0, q1: Qubit = 1)

A 2-qubit 01 phase-shift gate

\[\text{CPHASE01}(\theta) \equiv \text{diag}(1, e^{i \theta}, 1, 1)\]
class quantumflow.CPHASE10(theta: float, q0: Qubit = 0, q1: Qubit = 1)

A 2-qubit 10 phase-shift gate

\[\text{CPHASE10}(\theta) \equiv \text{diag}(1, 1, e^{i \theta}, 1)\]
class quantumflow.CPHASE(theta: float, q0: Qubit = 0, q1: Qubit = 1)

A 2-qubit 11 phase-shift gate

\[\text{CPHASE}(\theta) \equiv \text{diag}(1, 1, 1, e^{i \theta})\]
class quantumflow.PSWAP(theta: float, q0: Qubit = 0, q1: Qubit = 1)

A 2-qubit parametric-swap gate, as defined by Quil.

Locally equivalent to CANONICAL(1/2, 1/2, t)

\[\begin{split}\text{PSWAP}(\theta) \equiv \begin{pmatrix} 1&0&0&0 \\ 0&0&e^{i\phi}&0 \\ 0&e^{i\phi}&0&0 \\ 0&0&0&1 \end{pmatrix}\end{split}\]

Standard three-qubit gates

class quantumflow.CCNOT(q0: Qubit = 0, q1: Qubit = 1, q2: Qubit = 2)

A 3-qubit Toffoli gate. A controlled, controlled-not.

Equivalent to controlled_gate(cnot())

\[\begin{split}\text{CCNOT}() \equiv \begin{pmatrix} 1& 0& 0& 0& 0& 0& 0& 0 \\ 0& 1& 0& 0& 0& 0& 0& 0 \\ 0& 0& 1& 0& 0& 0& 0& 0 \\ 0& 0& 0& 1& 0& 0& 0& 0 \\ 0& 0& 0& 0& 1& 0& 0& 0 \\ 0& 0& 0& 0& 0& 1& 0& 0 \\ 0& 0& 0& 0& 0& 0& 0& 1 \\ 0& 0& 0& 0& 0& 0& 1& 0 \end{pmatrix}\end{split}\]
class quantumflow.CSWAP(q0: Qubit = 0, q1: Qubit = 1, q2: Qubit = 2)

A 3-qubit Fredkin gate. A controlled swap.

Equivalent to controlled_gate(swap())

\[\begin{split}\text{CSWAP}() \equiv \begin{pmatrix} 1& 0& 0& 0& 0& 0& 0& 0 \\ 0& 1& 0& 0& 0& 0& 0& 0 \\ 0& 0& 1& 0& 0& 0& 0& 0 \\ 0& 0& 0& 1& 0& 0& 0& 0 \\ 0& 0& 0& 0& 1& 0& 0& 0 \\ 0& 0& 0& 0& 0& 0& 1& 0 \\ 0& 0& 0& 0& 0& 1& 0& 0 \\ 0& 0& 0& 0& 0& 0& 0& 1 \end{pmatrix}\end{split}\]

Additional gates

One-qubit gates

class quantumflow.S_H(q0: Qubit = 0)

The inverse of the 1-qubit phase S gate, equivalent to PHASE(-pi/2).

\[\begin{split}\begin{pmatrix} 1 & 0 \\ 0 & -i \end{pmatrix}\end{split}\]
class quantumflow.T_H(q0: Qubit = 0)

The inverse (complex conjugate) of the 1-qubit T (pi/8) gate, equivalent to PHASE(-pi/4).

\[\begin{split}\begin{pmatrix} 1 & 0 \\ 0 & e^{-i \pi / 4} \end{pmatrix}\end{split}\]
class quantumflow.TX(t: float, q0: Qubit = 0)

Powers of the 1-qubit Pauli-X gate.

\[TX(t) = X^t = e^{i \pi t/2} R_X(\pi t)\]
Parameters:t – Number of half turns (quarter cycles) on Block sphere
class quantumflow.TY(t: float, q0: Qubit = 0)

Powers of the 1-qubit Pauli-Y gate.

The pseudo-Hadamard gate is TY(3/2), and its inverse is TY(1/2).

\[TY(t) = Y^t = e^{i \pi t/2} R_Y(\pi t)\]
Parameters:t – Number of half turns (quarter cycles) on Block sphere
class quantumflow.TZ(t: float, q0: Qubit = 0)

Powers of the 1-qubit Pauli-Z gate.

\[TZ(t) = Z^t = e^{i \pi t/2} R_Z(\pi t)\]
Parameters:t – Number of half turns (quarter cycles) on Block sphere
class quantumflow.TH(t: float, q0: Qubit = 0)

Powers of the 1-qubit Hadamard gate.

\[\begin{split}TH(t) = H^t = e^{i \pi t/2} \begin{pmatrix} \cos(\tfrac{t}{2}) + \tfrac{i}{\sqrt{2}}\sin(\tfrac{t}{2})) & \tfrac{i}{\sqrt{2}} \sin(\tfrac{t}{2}) \\ \tfrac{i}{\sqrt{2}} \sin(\tfrac{t}{2}) & \cos(\tfrac{t}{2}) -\tfrac{i}{\sqrt{2}} \sin(\frac{t}{2}) \end{pmatrix}\end{split}\]
class quantumflow.ZYZ(t0: float, t1: float, t2: float, q0: Qubit = 0)

A Z-Y-Z decomposition of one-qubit rotations in the Bloch sphere

The ZYZ decomposition of one-qubit rotations is

\[\text{ZYZ}(t_0, t_1, t_2) = Z^{t_2} Y^{t_1} Z^{t_0}\]

This is the unitary group on a 2-dimensional complex vector space, SU(2).

Ref: See Barenco et al (1995) section 4 (Warning: gates are defined as conjugate of what we now use?), or Eq 4.11 of Nielsen and Chuang.

Parameters:
  • t0 – Parameter of first parametric Z gate. Number of half turns on Block sphere.
  • t1 – Parameter of parametric Y gate.
  • t2 – Parameter of second parametric Z gate.
class quantumflow.P0(q0: Qubit = 0)

Project qubit to zero.

A non-unitary gate that represents the effect of a measurement. The norm of the resultant state is multiplied by the probability of observing 0.

class quantumflow.P1(q0: Qubit = 0)

Project qubit to one.

A non-unitary gate that represents the effect of a measurement. The norm of the resultant state is multiplied by the probability of observing 1.

Two-qubit gates

class quantumflow.CANONICAL(tx: float, ty: float, tz: float, q0: Qubit = 0, q1: Qubit = 1)

A canonical 2-qubit gate

The canonical decomposition of 2-qubits gates removes local 1-qubit rotations, and leaves only the non-local interactions.

\[\text{CANONICAL}(t_x, t_y, t_z) \equiv \exp\Big\{-i\frac{\pi}{2}(t_x X\otimes X + t_y Y\otimes Y + t_z Z\otimes Z)\Big\}\]
class quantumflow.XX(t: float, q0: Qubit = 0, q1: Qubit = 1)

A parametric 2-qubit gate generated from an XX interaction,

Equivalent to CANONICAL(t,0,0).

XX(1/2) is the Mølmer-Sørensen gate.

Ref: Sørensen, A. & Mølmer, K. Quantum computation with ions in thermal motion. Phys. Rev. Lett. 82, 1971–1974 (1999)

Parameters:t
class quantumflow.YY(t: float, q0: Qubit = 0, q1: Qubit = 1)

A parametric 2-qubit gate generated from a YY interaction.

Equivalent to CANONICAL(0,t,0), and locally equivalent to CANONICAL(t,0,0)

Parameters:t
class quantumflow.ZZ(t: float, q0: Qubit = 0, q1: Qubit = 1)

A parametric 2-qubit gate generated from a ZZ interaction.

Equivalent to CANONICAL(0,0,t), and locally equivalent to CANONICAL(t,0,0)

Parameters:t
class quantumflow.PISWAP(theta: float, q0: Qubit = 0, q1: Qubit = 1)

A parametric iswap gate, generated from XY interaction.

Locally equivalent to CANONICAL(t,t,0), where t = theta / (2 * pi)

\[\begin{split}\text{PISWAP}(\theta) \equiv \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & \cos(2\theta) & i \sin(2\theta) & 0 \\ 0 & i \sin(2\theta) & \cos(2\theta) & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix}\end{split}\]
class quantumflow.EXCH(t: float, q0: Qubit = 0, q1: Qubit = 1)

A 2-qubit parametric gate generated from an exchange interaction.

Equivalent to CANONICAL(t,t,t)

Multi-qubit gates

quantumflow.identity_gate(qubits: Union[int, Qubits]) → qf.Gate

Returns the K-qubit identity gate

quantumflow.random_gate(qubits: Union[int, Qubits]) → qf.Gate

Returns a random unitary gate on K qubits.

Ref:
“How to generate random matrices from the classical compact groups” Francesco Mezzadri, math-ph/0609050