Gate Zoo

View on QuantumAI Run in Google Colab View source on GitHub Download notebook

Setup

try:
    import cirq
except ImportError:
    print("installing cirq...")
    !pip install --quiet --pre cirq
    print("installed cirq.")

import IPython.display as ipd
import cirq
import inspect

def display_gates(*gates):
    for gate_name in gates:
        ipd.display(ipd.Markdown("---"))
        gate = getattr(cirq, gate_name)
        ipd.display(ipd.Markdown(f"#### cirq.{gate_name}"))
        ipd.display(ipd.Markdown(inspect.cleandoc(gate.__doc__ or "")))
    else:
        ipd.display(ipd.Markdown("---"))

Cirq comes with many gates that are standard across quantum computing. This notebook serves as a reference sheet for these gates.

Single Qubit Gates

Gate Constants

Cirq defines constants which are gate instances for particular important single qubit gates.

display_gates("X", "Y", "Z", "H", "S", "T")

cirq.X

The Pauli X gate.

This is the exponent=1 instance of the cirq.XPowGate.

The untary matrix of cirq.X is:

\[ \begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix} \]


cirq.Y

The Pauli Y gate.

This is the exponent=1 instance of the cirq.YPowGate.

The unitary matrix of cirq.Y is:

\[ \begin{bmatrix} 0 & -i \\ i & 0 \end{bmatrix} \]


cirq.Z

The Pauli Z gate.

This is the exponent=1 instance of the cirq.ZPowGate.

The unitary matrix of cirq.Z is:

\[ \begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix} \]


cirq.H

The Hadamard gate.

The exponent=1 instance of cirq.HPowGate.

The unitary matrix of cirq.H is:

\[ \begin{bmatrix} \frac{1}{\sqrt{2} } & \frac{1}{\sqrt{2} } \\ \frac{1}{\sqrt{2} } & -\frac{1}{\sqrt{2} } \end{bmatrix} \]


cirq.S

The Clifford S gate.

The exponent=0.5 instance of cirq.ZPowGate.

The unitary matrix of cirq.S is:

\[ \begin{bmatrix} 1 & 0 \\ 0 & i \end{bmatrix} \]


cirq.T

The non-Clifford T gate.

The exponent=0.25 instance of cirq.ZPowGate.

The unitary matrix of cirq.T is

\[ \begin{bmatrix} 1 & 0 \\ 0 & e^{i \pi /4} \end{bmatrix} \]


Traditional Pauli Rotation Gates

Cirq defines traditional single qubit rotations that are rotations in radiants abougt different Pauli directions.

display_gates("Rx", "Ry", "Rz")

cirq.Rx

A gate with matrix \(e^{-i X t/2}\) that rotates around the X axis of the Bloch sphere by \(t\).

The unitary matrix of cirq.Rx(rads=t) is:

\[ e^{-i X t /2} = \begin{bmatrix} \cos(t/2) & -i \sin(t/2) \\ -i \sin(t/2) & \cos(t/2) \end{bmatrix} \]

This gate corresponds to the traditionally defined rotation matrices about the Pauli X axis.


cirq.Ry

A gate with matrix \(e^{-i Y t/2}\) that rotates around the Y axis of the Bloch sphere by \(t\).

The unitary matrix of cirq.Ry(rads=t) is:

\[ e^{-i Y t / 2} = \begin{bmatrix} \cos(t/2) & -\sin(t/2) \\ \sin(t/2) & \cos(t/2) \end{bmatrix} \]

This gate corresponds to the traditionally defined rotation matrices about the Pauli Y axis.


cirq.Rz

A gate with matrix \(e^{-i Z t/2}\) that rotates around the Z axis of the Bloch sphere by \(t\).

The unitary matrix of cirq.Rz(rads=t) is:

\[ e^{-i Z t /2} = \begin{bmatrix} e^{-it/2} & 0 \\ 0 & e^{it/2} \end{bmatrix} \]

This gate corresponds to the traditionally defined rotation matrices about the Pauli Z axis.


Pauli PowGates

If you think of the cirq.Z gate as phasing the state \(|1\rangle\) by \(-1\), then you might think that the square root of this gate phases the state \(|1\rangle\) by \(i=\sqrt{-1}\). The XPowGate, YPowGate and ZPowGates all act in this manner, phasing the state corresponding to their \(-1\) eigenvalue by a prescribed amount. This ends up being the same as the Rx, Ry, and Rz up to a global phase.

display_gates("XPowGate", "YPowGate", "ZPowGate")

cirq.XPowGate

A gate that rotates around the X axis of the Bloch sphere.

The unitary matrix of cirq.XPowGate(exponent=t, global_shift=s) is:

\[ e^{i \pi t (s + 1/2)} \begin{bmatrix} \cos(\pi t /2) & -i \sin(\pi t /2) \\ -i \sin(\pi t /2) & \cos(\pi t /2) \end{bmatrix} \]

Note in particular that this gate has a global phase factor of \(e^{i \pi t / 2}\) vs the traditionally defined rotation matrices about the Pauli X axis. See cirq.Rx for rotations without the global phase. The global phase factor can be adjusted by using the global_shift parameter when initializing.

cirq.X, the Pauli X gate, is an instance of this gate at exponent=1.


cirq.YPowGate

A gate that rotates around the Y axis of the Bloch sphere.

The unitary matrix of cirq.YPowGate(exponent=t) is:

\[ \begin{bmatrix} e^{i \pi t /2} \cos(\pi t /2) & - e^{i \pi t /2} \sin(\pi t /2) \\ e^{i \pi t /2} \sin(\pi t /2) & e^{i \pi t /2} \cos(\pi t /2) \end{bmatrix} \]

Note in particular that this gate has a global phase factor of \(e^{i \pi t / 2}\) vs the traditionally defined rotation matrices about the Pauli Y axis. See cirq.Ry for rotations without the global phase. The global phase factor can be adjusted by using the global_shift parameter when initializing.

cirq.Y, the Pauli Y gate, is an instance of this gate at exponent=1.

Unlike cirq.XPowGate and cirq.ZPowGate, this gate has no generalization to qudits and hence does not take the dimension argument. Ignoring the global phase all generalized Pauli operators on a d-level system may be written as Xa Zb for a,b=0,1,...,d-1. For a qubit, there is only one "mixed" operator: XZ, conventionally denoted -iY. However, when d > 2 there are (d-1)*(d-1) > 1 such "mixed" operators (still ignoring the global phase). Due to this ambiguity, qudit Y gate is not well defined. The "mixed" operators for qudits are generally not referred to by name, but instead are specified in terms of X and Z.


cirq.ZPowGate

A gate that rotates around the Z axis of the Bloch sphere.

The unitary matrix of cirq.ZPowGate(exponent=t, global_shift=s) is:

\[ e^{i \pi s t} \begin{bmatrix} 1 & 0 \\ 0 & e^{i \pi t} \end{bmatrix} \]

Note in particular that this gate has a global phase factor of \(e^{i\pi t/2}\) vs the traditionally defined rotation matrices about the Pauli Z axis. See cirq.Rz for rotations without the global phase. The global phase factor can be adjusted by using the global_shift parameter when initializing.

cirq.Z, the Pauli Z gate, is an instance of this gate at exponent=1.


More Single Qubit Gate

Many quantum computing implementations use qubits whose energy eigenstate are the computational basis states. In these cases it is often useful to move cirq.ZPowGate's through other single qubit gates, "phasing" the other gates. For these scenarios, the following phased gates are useful.

display_gates("PhasedXPowGate", "PhasedXZGate", "HPowGate")

cirq.PhasedXPowGate

A gate equivalent to \(Z^{p} X^t Z^{-p}\).

The unitary matrix of cirq.PhasedXPowGate(exponent=t, phase_exponent=p) is:

\[ \begin{bmatrix} e^{i \pi t /2} \cos(\pi t/2) & -i e^{i \pi (t /2 - p)} \sin(\pi t /2) \\ -i e^{i \pi (t /2 + p)} \sin(\pi t /2) & e^{i \pi t /2} \cos(\pi t/2) \end{bmatrix} \]

This gate is like an cirq.XPowGate, but which has been "phased", by applying a cirq.ZPowGate before and after this gate. In the language of the Bloch sphere, \(p\) determines the axis in the XY plane about which a rotation of amount determined by \(t\) occurs.


cirq.PhasedXZGate

A single qubit gate equivalent to the circuit \(Z^z Z^{a} X^x Z^{-a}\).

The unitary matrix of cirq.PhasedXZGate(x_exponent=x, z_exponent=z, axis_phase_exponent=a) is:

\[ \begin{bmatrix} e^{i \pi x / 2} \cos(\pi x /2) & -i e^{i \pi (x/2 - a)} \sin(\pi x / 2) \\ -i e^{i \pi (x/2 + z + a)} \sin(\pi x / 2) & e^{i \pi (x / 2 + z)} \cos(\pi x /2) \end{bmatrix} \]

This gate can be thought of as a cirq.PhasedXPowGate followed by a cirq.ZPowGate.

The axis phase exponent (\(a\)) decides which axis in the XY plane to rotate around. The amount of rotation around that axis is decided by the x exponent (\(x\)). Then the z exponent (\(z\)) decides how much to finally phase the qubit.

Every single qubit gate can be written as a single cirq.PhasedXZGate.


cirq.HPowGate

A Gate that performs a rotation around the X+Z axis of the Bloch sphere.

The unitary matrix of cirq.HPowGate(exponent=t) is:

\[ \begin{bmatrix} e^{i\pi t/2} \left(\cos(\pi t/2) - i \frac{\sin (\pi t /2)}{\sqrt{2} }\right) && -i e^{i\pi t/2} \frac{\sin(\pi t /2)}{\sqrt{2} } \\ -i e^{i\pi t/2} \frac{\sin(\pi t /2)}{\sqrt{2} } && e^{i\pi t/2} \left(\cos(\pi t/2) + i \frac{\sin (\pi t /2)}{\sqrt{2} }\right) \end{bmatrix} \]

Note in particular that for \(t=1\), this gives the Hadamard matrix

\[ \begin{bmatrix} \frac{1}{\sqrt{2} } & \frac{1}{\sqrt{2} } \\ \frac{1}{\sqrt{2} } & -\frac{1}{\sqrt{2} } \end{bmatrix} \]

cirq.H, the Hadamard gate, is an instance of this gate at exponent=1.


Two Qubit Gates

Gate Constants

Cirq defines convenient constants for common two qubit gates.

display_gates("CX", "CZ", "SWAP", "ISWAP", "ISWAP_INV", "SQRT_ISWAP", "SQRT_ISWAP_INV")

cirq.CX

The controlled NOT gate.

This is the exponent=1 instance of cirq.CXPowGate.

Alternative name: cirq.CNOT.

The unitary matrix of this gate is (empty elements are \(0\)):

\[ \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0 \end{bmatrix} \]


cirq.CZ

The controlled Z gate.

This is the exponent=1 instance of cirq.CZPowGate.

The unitary matrix of this gate is (empty elements are \(0\)):

\[ \begin{bmatrix} 1 & & & \\ & 1 & & \\ & & 1 & \\ & & & -1 \end{bmatrix} \]


cirq.SWAP

The swap gate.

This gate will swap two qubits (in any basis).

The unitary matrix of this gate is:

\[ \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} \]


cirq.ISWAP

The iswap gate.

The unitary matrix of this gate is:

\[ \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & i & 0 \\ 0 & i & 0 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} \]


cirq.ISWAP_INV

The inverse of the iswap gate.

The unitary matrix of this gate is:

\[ \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & -i & 0 \\ 0 & -i & 0 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} \]


cirq.SQRT_ISWAP

The square root of iswap gate.

The unitary matrix of this gate is:

\[ \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & \frac{1}{\sqrt{2} } & \frac{i}{\sqrt{2} } & 0 \\ 0 & \frac{i}{\sqrt{2} } & \frac{1}{\sqrt{2} } & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} \]


cirq.SQRT_ISWAP_INV

The inverse square root of iswap gate.

The unitary matrix of this gate is:

\[ \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & \frac{1}{\sqrt{2} } & -\frac{i}{\sqrt{2} } & 0 \\ 0 & -\frac{i}{\sqrt{2} } & \frac{1}{\sqrt{2} } & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} \]


Parity Gates

If \(P\) is a non-identity Pauli matrix, then it has eigenvalues \(\pm 1\). \(P \otimes P\) similarly has eigenvalues \(\pm 1\) which are the product of the eigenvalues of the single \(P\) eigenvalues. In this sense, \(P \otimes P\) has an eigenvalue which encodes the parity of the eigenvalues of the two qubits. If you think of \(P \otimes P\) as phasing its \(-1\) eigenvectors by \(-1\), then you could consider \((P \otimes P)^{\frac{1}{2} }\) as the gate that phases the \(-1\) eigenvectors by \(\sqrt{-1} =i\). The Parity gates are exactly these gates for the three different non-identity Paulis.

display_gates("XXPowGate", "YYPowGate", "ZZPowGate")

cirq.XXPowGate

The X-parity gate, possibly raised to a power.

The XX**t gate implements the following unitary:

\[ (X \otimes X)^t = \begin{bmatrix} c & 0 & 0 & s \\ 0 & c & s & 0 \\ 0 & s & c & 0 \\ s & 0 & 0 & c \end{bmatrix} \]

where

\[ c = f \cos\left(\frac{\pi t}{2}\right) \]

\[ s = -i f \sin\left(\frac{\pi t}{2}\right) \]

\[ f = e^{\frac{i \pi t}{2} }. \]

See also: cirq.ops.MSGate (the Mølmer–Sørensen gate), which is implemented via this class.


cirq.YYPowGate

The Y-parity gate, possibly raised to a power.

The YY**t gate implements the following unitary:

\[ (Y \otimes Y)^t = \begin{bmatrix} c & 0 & 0 & -s \\ 0 & c & s & 0 \\ 0 & s & c & 0 \\ -s & 0 & 0 & c \\ \end{bmatrix} \]

where

\[ c = f \cos\left(\frac{\pi t}{2}\right) \]

\[ s = -i f \sin\left(\frac{\pi t}{2}\right) \]

\[ f = e^{\frac{i \pi t}{2} }. \]


cirq.ZZPowGate

The Z-parity gate, possibly raised to a power.

The ZZ**t gate implements the following unitary:

\[ (Z \otimes Z)^t = \begin{bmatrix} 1 & & & \\ & e^{i \pi t} & & \\ & & e^{i \pi t} & \\ & & & 1 \end{bmatrix} \]


There are also constants that one can use to define the parity gates via exponentiating them.

display_gates("XX", "YY", "ZZ")

cirq.XX

The tensor product of two X gates.

Useful for creating cirq.XXPowGates via cirq.XX**t.

This is the exponent=1 instance of cirq.XXPowGate.


cirq.YY

The tensor product of two Y gates.

Useful for creating cirq.YYPowGates via cirq.YY**t.

This is the exponent=1 instance of cirq.YYPowGate.


cirq.ZZ

The tensor product of two Z gates.

Useful for creating cirq.ZZPowGates via cirq.ZZ**t.

This is the exponent=1 instance of cirq.ZZPowGate.


Fermionic Gates

If we think of \(|1\rangle\) as an excitation, then the gates that preserve the number of excitations are the fermionic gates. There are two implementations, with differing phase conventions.

display_gates("FSimGate", "PhasedFSimGate")

cirq.FSimGate

Fermionic simulation gate.

Contains all two qubit interactions that preserve excitations, up to single-qubit rotations and global phase.

The unitary matrix of this gate is:

\[ \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & a & b & 0 \\ 0 & b & a & 0 \\ 0 & 0 & 0 & c \end{bmatrix} \]

where:

\[ a = \cos(\theta) \]

\[ b = -i \sin(\theta) \]

\[ c = e^{i \phi} \]

Note the difference in sign conventions between FSimGate and the ISWAP and CZPowGate:

FSimGate(θ, φ) = ISWAP**(-2θ/π) CZPowGate(exponent=-φ/π)


cirq.PhasedFSimGate

General excitation-preserving two-qubit gate.

The unitary matrix of PhasedFSimGate(θ, ζ, χ, γ, φ) is:

\[ \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & e^{-i \gamma - i \zeta} \cos(\theta) & -i e^{-i \gamma + i\chi} \sin(\theta) & 0 \\ 0 & -i e^{-i \gamma - i \chi} \sin(\theta) & e^{-i \gamma + i \zeta} \cos(\theta) & 0 \\ 0 & 0 & 0 & e^{-2i \gamma - i \phi} \end{bmatrix} \]

This parametrization follows eq (18) in https://arxiv.org/abs/2010.07965 See also eq (43) in https://arxiv.org/abs/1910.11333 for an older variant which uses the same θ and φ parameters, but has three phase angles that have different names and opposite sign. Specifically, ∆+ angle corresponds to -γ, ∆- corresponds to -ζ and ∆-,off corresponds to -χ.

Another useful parametrization of PhasedFSimGate is based on the fact that the gate is equivalent up to global phase to the following circuit:

0: ───Rz(α0)───FSim(θ, φ)───Rz(β0)───
               │
1: ───Rz(α1)───FSim(θ, φ)───Rz(β1)───

where α0 and α1 are Rz angles to be applied before the core FSimGate, β0 and β1 are Rz angles to be applied after FSimGate and θ and φ specify the core FSimGate. Use the static factory function from_fsim_rz to instantiate the gate using this parametrization.

Note that the θ and φ parameters in the two parametrizations are the same.

The matrix above is block diagonal where the middle block may be any element of U(2) and the bottom right block may be any element of U(1). Consequently, five real parameters are required to specify an instance of PhasedFSimGate. Therefore, the second parametrization is not injective. Indeed, for any angle δ

cirq.PhasedFSimGate.from_fsim_rz(θ, φ, (α0, α1), (β0, β1))

and

cirq.PhasedFSimGate.from_fsim_rz(θ, φ,
                                 (α0 + δ, α1 + δ),
                                 (β0 - δ, β1 - δ))

specify the same gate and therefore the two instances will compare as equal up to numerical error. Another consequence of the non-injective character of the second parametrization is the fact that the properties rz_angles_before and rz_angles_after may return different Rz angles than the ones used in the call to from_fsim_rz.

This gate is generally not symmetric under exchange of qubits. It becomes symmetric if both of the following conditions are satisfied:

  • ζ = kπ or θ = π/2 + lπ for k and l integers,
  • χ = kπ or θ = lπ for k and l integers.

Two qubit PowGates

Just as cirq.XPowGate represents a powering of cirq.X, our two qubit gate constants also have corresponding "Pow" versions.

display_gates("SwapPowGate", "ISwapPowGate", "CZPowGate", "CXPowGate", "PhasedISwapPowGate")

cirq.SwapPowGate

The SWAP gate, possibly raised to a power. Exchanges qubits.

SwapPowGate()**t = SwapPowGate(exponent=t) and acts on two qubits in the computational basis as the matrix:

\[ \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & g c & -i g s & 0 \\ 0 & -i g s & g c & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} \]

where:

\[ c = \cos\left(\frac{\pi t}{2}\right) \]

\[ s = \sin\left(\frac{\pi t}{2}\right) \]

\[ g = e^{\frac{i \pi t}{2} } \]

cirq.SWAP, the swap gate, is an instance of this gate at exponent=1.


cirq.ISwapPowGate

Rotates the |01⟩ vs |10⟩ subspace of two qubits around its Bloch X-axis.

When exponent=1, swaps the two qubits and phases |01⟩ and |10⟩ by i. More generally, this gate's matrix is defined as follows:

ISWAP**t ≡ exp(+i π t (X⊗X + Y⊗Y) / 4)

which is given by the matrix:

\[ \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & c & i s & 0 \\ 0 & i s & c & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} \]

where

\[ c = \cos\left(\frac{\pi t}{2}\right) \]

\[ s = \sin\left(\frac{\pi t}{2}\right) \]

cirq.ISWAP, the swap gate that applies i to the |01⟩ and |10⟩ states, is an instance of this gate at exponent=1.

References: "What is the matrix of the iSwap gate?" https://quantumcomputing.stackexchange.com/questions/2594/


cirq.CZPowGate

A gate that applies a phase to the |11⟩ state of two qubits.

The unitary matrix of CZPowGate(exponent=t) is:

\[ \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & e^{i \pi t} \\ \end{bmatrix} \]

cirq.CZ, the controlled Z gate, is an instance of this gate at exponent=1.


cirq.CXPowGate

A gate that applies a controlled power of an X gate.

When applying CNOT (controlled-not) to qubits, you can either use positional arguments CNOT(q1, q2), where q2 is toggled when q1 is on, or named arguments CNOT(control=q1, target=q2). (Mixing the two is not permitted.)

The unitary matrix of cirq.CXPowGate(exponent=t) is:

\[ \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & g c & -i g s \\ 0 & 0 & -i g s & g c \end{bmatrix} \]

where:

\[ c = \cos\left(\frac{\pi t}{2}\right) \]

\[ s = \sin\left(\frac{\pi t}{2}\right) \]

\[ g = e^{\frac{i \pi t}{2} } \]

cirq.CNOT, the controlled NOT gate, is an instance of this gate at exponent=1.


cirq.PhasedISwapPowGate

Fractional ISWAP conjugated by Z rotations.

PhasedISwapPowGate with phase_exponent p and exponent t is equivalent to the composition

(Z^-p ⊗ Z^p) ISWAP^t (Z^p ⊗ Z^-p)

and is given by the matrix:

\[ \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & c & i s f & 0 \\ 0 & i s f^* & c & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} \]

where:

\[ c = \cos\left(\frac{\pi t}{2}\right) \]

\[ s = \sin\left(\frac{\pi t}{2}\right) \]

\[ f = e^{2 \pi p i} \]


Three Qubit Gates

Gate Constants

Cirq provides constants for common three qubit gates.

display_gates("CCX", "CCZ", "CSWAP")

cirq.CCX

The Tofolli gate, also known as the Controlled-Controlled-X gate.

If the first two qubits are in the |11⟩ state, this flips the third qubit in the computational basis, otherwise this applies identity to the third qubit.

The exponent=1 instance of cirq.CCXPowGate.

The unitary matrix of this gate is (empty elements are \(0\)):

\[ \begin{bmatrix} 1 & & & & & & & \\ & 1 & & & & & & \\ & & 1 & & & & & \\ & & & 1 & & & & \\ & & & & 1 & & & \\ & & & & & 1 & & \\ & & & & & & 0 & 1 \\ & & & & & & 1 & 0 \end{bmatrix} \]

Alternative names: cirq.CCNOT and cirq.TOFFOLI.


cirq.CCZ

The Controlled-Controlled-Z gate.

The exponent=1 instance of cirq.CCZPowGate.

The unitary matrix of this gate is (empty elements are \(0\)):

\[ \begin{bmatrix} 1 & & & & & & & \\ & 1 & & & & & & \\ & & 1 & & & & & \\ & & & 1 & & & & \\ & & & & 1 & & & \\ & & & & & 1 & & \\ & & & & & & 1 & \\ & & & & & & & -1 \end{bmatrix} \]


cirq.CSWAP

The Controlled Swap gate, also known as the Fredkin gate.

If the first qubit is |1⟩, this applies a SWAP between the second and third qubit, otherwise it acts as identity on the second and third qubit.

An instance of cirq.CSwapGate.

The unitary matrix of this gate is (empty elements are \(0\)):

\[ \begin{bmatrix} 1 & & & & & & & \\ & 1 & & & & & & \\ & & 1 & & & & & \\ & & & 1 & & & & \\ & & & & 1 & & & \\ & & & & & 0 & 1 & \\ & & & & & 1 & 0 & \\ & & & & & & & 1 \end{bmatrix} \]

Alternative names: cirq.FREDKIN.


Three Qubit Pow Gates

Corresponding to some of the above gate constants are the corresponding PowGates.

display_gates("CCXPowGate", "CCZPowGate")

cirq.CCXPowGate

A Toffoli (doubly-controlled-NOT) that can be raised to a power.

The unitary matrix of CCX**t is an 8x8 identity except the bottom right 2x2 area is the matrix of X**t:

\[ \begin{bmatrix} 1 & & & & & & & \\ & 1 & & & & & & \\ & & 1 & & & & & \\ & & & 1 & & & & \\ & & & & 1 & & & \\ & & & & & 1 & & \\ & & & & & & e^{i \pi t /2} \cos(\pi t) & -i e^{i \pi t /2} \sin(\pi t) \\ & & & & & & -i e^{i \pi t /2} \sin(\pi t) & e^{i \pi t /2} \cos(\pi t) \end{bmatrix} \]


cirq.CCZPowGate

A doubly-controlled-Z that can be raised to a power.

The unitary matrix of CCZ**t is (empty elements are \(0\)):

\[ \begin{bmatrix} 1 & & & & & & & \\ & 1 & & & & & & \\ & & 1 & & & & & \\ & & & 1 & & & & \\ & & & & 1 & & & \\ & & & & & 1 & & \\ & & & & & & 1 & \\ & & & & & & & e^{i \pi t} \end{bmatrix} \]


N Qubit Gates

Do Nothing Gates

Sometimes you just want a gate to represent doing nothing.

display_gates("IdentityGate", "WaitGate")

cirq.IdentityGate

A Gate that perform no operation on qubits.

The unitary matrix of this gate is a diagonal matrix with all 1s on the diagonal and all 0s off the diagonal in any basis.

cirq.I is the single qubit identity gate.


cirq.WaitGate

An idle gate that represents waiting.

In non-noisy simulators, this gate is just an identity gate. But noisy simulators and noise models may insert more error for longer waits.


Measurement Gates

Measurement gates are gates that represent a measurement and can operate on any number of qubits.

display_gates("MeasurementGate")

cirq.MeasurementGate

A gate that measures qubits in the computational basis.

The measurement gate contains a key that is used to identify results of measurements.

Instead of constructing this directly, consider using the cirq.measure helper method.


Matrix Gates

If one has a specific unitary matrix in mind, then one can construct it using matrix gates, or, if the unitary is diagonal, the diagonal gates.

display_gates("MatrixGate", "DiagonalGate", "TwoQubitDiagonalGate", "ThreeQubitDiagonalGate")

cirq.MatrixGate

A unitary qubit or qudit gate defined entirely by its numpy matrix.

For example cirq.MatrixGate(np.array([[0, 1j], [1, 0]])) has the unitary matrix:

\[ \begin{bmatrix} 0 & i \\ 1 & 0 \end{bmatrix} \]


cirq.DiagonalGate

An n qubit gate which acts as phases on computational basis states.

This gate's off-diagonal elements are zero and its on-diagonal elements are all phases.


cirq.TwoQubitDiagonalGate

A two qubit gate whose unitary is a diagonal \(4 \times 4\) matrix.

This gate's off-diagonal elements are zero and its on-diagonal elements are all phases.

For example, cirq.TwoQubitDiagonalGate([0, 1, -1, 0]) has the unitary matrix

\[ \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & e^i & 0 & 0 \\ 0 & 0 & e^{-i} & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} \]


cirq.ThreeQubitDiagonalGate

A three qubit gate whose unitary is given by a diagonal \(8 \times 8\) matrix.

This gate's off-diagonal elements are zero and its on diagonal elements are all phases.


Pauli String Gates

Pauli strings are expressions like "XXZ" representing the Pauli operator X operating on the first two qubits, and Z on the last qubit, along with a numeric (or symbolic) coefficient. When the coefficient is a unit complex number, then this is a valid unitary gate. Similarly one can construct gates which phases the \(\pm 1\) eigenvalues of such a Pauli string.

display_gates("DensePauliString", "MutableDensePauliString", "PauliStringPhasorGate")

cirq.DensePauliString

An immutable string of Paulis, like XIXY, with a coefficient.

A DensePauliString represents a multi-qubit pauli operator, i.e. a tensor product of single qubits Pauli gates (including the cirq.IdentityGate), each of which would act on a different qubit. When applied on qubits, a DensePauliString results in cirq.PauliString as an operation.

Note that cirq.PauliString only stores a tensor product of non-identity cirq.Pauli operations whereas cirq.DensePauliString also supports storing the cirq.IdentityGate.

For example,

dps = cirq.DensePauliString('XXIY') print(dps) # 4 qubit pauli operator with 'X' on first 2 qubits, 'I' on 3rd and 'Y' on 4th. +XXIY ps = dps.on(cirq.LineQubit.range(4)) # When applied on qubits, we get a cirq.PauliString. print(ps) # Note that cirq.PauliString only preserves non-identity operations. X(q(0))X(q(1))*Y(q(3))

This can optionally take a coefficient, for example:

dps = cirq.DensePauliString("XX", coefficient=3) print(dps) # Represents 3 times the operator XX acting on two qubits. (3+0j)XX print(dps.on(cirq.LineQubit.range(2))) # Coefficient is propagated to cirq.PauliString. (3+0j)X(q(0))X(q(1))

If the coefficient has magnitude of 1, the resulting operator is a unitary and thus is also a cirq.Gate.

Note that DensePauliString is an immutable object. If you need a mutable version of dense pauli strings, see cirq.MutableDensePauliString.


cirq.MutableDensePauliString

A mutable string of Paulis, like XIXY, with a coefficient.

cirq.MutableDensePauliString is a mutable version of cirq.DensePauliString. It exists mainly to help mutate dense pauli strings efficiently, instead of always creating a copy, and then converting back to a frozen cirq.DensePauliString representation.

For example:

mutable_dps = cirq.MutableDensePauliString('XXZZ') mutable_dps[:2] = 'YY' # cirq.MutableDensePauliString supports item assignment. print(mutable_dps) +YYZZ (mutable)

See docstrings of cirq.DensePauliString for more details on dense pauli strings.


cirq.PauliStringPhasorGate

A gate that phases the eigenstates of a Pauli string.

The -1 eigenstates of the Pauli string will have their amplitude multiplied by e^(i pi exponent_neg) while +1 eigenstates of the Pauli string will have their amplitude multiplied by e^(i pi exponent_pos).


Algorithm Based Gates

It is useful to define composite gates which correspond to algorithmic primitives, i.e. one can think of the fourier transform as a single unitary gate.

display_gates("BooleanHamiltonianGate", "QuantumFourierTransformGate", "PhaseGradientGate")

cirq.BooleanHamiltonianGate

A gate that represents evolution due to a Hamiltonian from a set of Boolean functions.

This gate constructs a diagonal gate in the computational basis that encodes in its phases classical functions.

The gate is specified by a list of parameters, \([x_0, x_1, \dots, x_{n-1}]\), a list of boolean expressions that are functions of these parameters, \([f_0(x_0,\dots,x_{n-1}), f_1(x_0,\dots,x_{n-1}), \dots f_{p-1}(x_0,\dots,x_{n-1})]\) and an angle \(t\). For these parameters the gate is

\[ \sum_{x=0}^{2^n-1} e^{i \frac{t}{2} \sum_{k=0}^{p-1}f_k(x_0,\dots,x_{n-1})} |x\rangle\langle x| \]


cirq.QuantumFourierTransformGate

Switches from the computational basis to the frequency basis.

This gate has the unitary

\[ \frac{1}{2^{n/2} }\sum_{x,y=0}^{2^n-1} \omega^{xy} |x\rangle\langle y| \]

where

\[ \omega = e^{\frac{2\pi i}{2^n} } \]


cirq.PhaseGradientGate

Phases all computational basis states proportional to the integer value of the state.

The gate cirq.PhaseGradientGate(n, t) has the unitary

\[ \sum_{x=0}^{2^n-1} \omega^x |x\rangle \langle x| \]

where

\[ \omega=e^{2 \pi i/2^n} \]

This gate makes up a portion of the quantum fourier transform.


Classiscal Permutation Gates

Sometimes you want to represent shuffling of qubits.

display_gates("QubitPermutationGate")

cirq.QubitPermutationGate

A qubit permutation gate specified by a permutation list.

For a permutation list \([p_0, p_1,\dots,p_{n-1}]\) this gate has the unitary

\[ \sum_{x_0,x_1,\dots,x_{n-1} \in \{0, 1\} } |x_{p_0}, x_{p_1}, \dots, x_{p_{n-1} }\rangle \langle x_0, x_1, \dots, x_{n-1}| \]