Gates

An implementation of gates that act on qubits.

Gates are unitary operators that act on the space of qubits.

Medium Term Todo:

  • Optimize Gate._apply_operators_Qubit to remove the creation of many intermediate Qubit objects.
  • Add commutation relationships to all operators and use this in gate_sort.
  • Fix gate_sort and gate_simp.
  • Get multi-target UGates plotting properly.
  • Get UGate to work with either sympy/numpy matrices and output either format. This should also use the matrix slots.
class sympy.physics.quantum.gate.Gate[source]

Non-controlled unitary gate operator that acts on qubits.

This is a general abstract gate that needs to be subclassed to do anything useful.

Parameters :

label : tuple, int

A list of the target qubits (as ints) that the gate will apply to.

get_target_matrix(format='sympy')[source]

The matrix rep. of the target part of the gate.

Parameters :

format : str

The format string (‘sympy’,’numpy’, etc.)

min_qubits[source]

The minimum number of qubits this gate needs to act on.

nqubits[source]

The total number of qubits this gate acts on.

For controlled gate subclasses this includes both target and control qubits, so that, for examples the CNOT gate acts on 2 qubits.

targets[source]

A tuple of target qubits.

class sympy.physics.quantum.gate.CGate[source]

A general unitary gate with control qubits.

A general control gate applies a target gate to a set of targets if all of the control qubits have a particular values (set by CGate.control_value).

Parameters :

label : tuple

The label in this case has the form (controls, gate), where controls is a tuple/list of control qubits (as ints) and gate is a Gate instance that is the target operator.

controls[source]

A tuple of control qubits.

decompose(**options)[source]

Decompose the controlled gate into CNOT and single qubits gates.

eval_controls(qubit)[source]

Return True/False to indicate if the controls are satisfied.

gate[source]

The non-controlled gate that will be applied to the targets.

min_qubits[source]

The minimum number of qubits this gate needs to act on.

nqubits[source]

The total number of qubits this gate acts on.

For controlled gate subclasses this includes both target and control qubits, so that, for examples the CNOT gate acts on 2 qubits.

targets[source]

A tuple of target qubits.

class sympy.physics.quantum.gate.UGate[source]

General gate specified by a set of targets and a target matrix.

Parameters :

label : tuple

A tuple of the form (targets, U), where targets is a tuple of the target qubits and U is a unitary matrix with dimension of len(targets).

get_target_matrix(format='sympy')[source]

The matrix rep. of the target part of the gate.

Parameters :

format : str

The format string (‘sympy’,’numpy’, etc.)

targets[source]

A tuple of target qubits.

class sympy.physics.quantum.gate.OneQubitGate[source]

A single qubit unitary gate base class.

class sympy.physics.quantum.gate.TwoQubitGate[source]

A two qubit unitary gate base class.

class sympy.physics.quantum.gate.IdentityGate[source]

The single qubit identity gate.

Parameters :

target : int

The target qubit this gate will apply to.

class sympy.physics.quantum.gate.HadamardGate[source]

The single qubit Hadamard gate.

Parameters :

target : int

The target qubit this gate will apply to.

Examples

>>> from sympy import sqrt
>>> from sympy.physics.quantum.qubit import Qubit
>>> from sympy.physics.quantum.gate import HadamardGate
>>> from sympy.physics.quantum.qapply import qapply
>>> qapply(HadamardGate(0)*Qubit('1'))
sqrt(2)*|0>/2 - sqrt(2)*|1>/2
>>> # Hadamard on bell state, applied on 2 qubits.
>>> psi = 1/sqrt(2)*(Qubit('00')+Qubit('11'))
>>> qapply(HadamardGate(0)*HadamardGate(1)*psi)
sqrt(2)*|00>/2 + sqrt(2)*|11>/2
class sympy.physics.quantum.gate.XGate[source]

The single qubit X, or NOT, gate.

Parameters :

target : int

The target qubit this gate will apply to.

class sympy.physics.quantum.gate.YGate[source]

The single qubit Y gate.

Parameters :

target : int

The target qubit this gate will apply to.

class sympy.physics.quantum.gate.ZGate[source]

The single qubit Z gate.

Parameters :

target : int

The target qubit this gate will apply to.

class sympy.physics.quantum.gate.TGate[source]

The single qubit pi/8 gate.

This gate rotates the phase of the state by pi/4 if the state is |1> and does nothing if the state is |0>.

Parameters :

target : int

The target qubit this gate will apply to.

class sympy.physics.quantum.gate.PhaseGate[source]

The single qubit phase, or S, gate.

This gate rotates the phase of the state by pi/2 if the state is |1> and does nothing if the state is |0>.

Parameters :

target : int

The target qubit this gate will apply to.

class sympy.physics.quantum.gate.SwapGate[source]

Two qubit SWAP gate.

This gate swap the values of the two qubits.

Parameters :

label : tuple

A tuple of the form (target1, target2).

decompose(**options)[source]

Decompose the SWAP gate into CNOT gates.

class sympy.physics.quantum.gate.CNotGate[source]

Two qubit controlled-NOT.

This gate performs the NOT or X gate on the target qubit if the control qubits all have the value 1.

Parameters :

label : tuple

A tuple of the form (control, target).

Examples

>>> from sympy.physics.quantum.gate import CNOT
>>> from sympy.physics.quantum.qapply import qapply
>>> from sympy.physics.quantum.qubit import Qubit
>>> c = CNOT(1,0)
>>> qapply(c*Qubit('10')) # note that qubits are indexed from right to left
|11>
controls[source]

A tuple of control qubits.

gate[source]

The non-controlled gate that will be applied to the targets.

min_qubits[source]

The minimum number of qubits this gate needs to act on.

targets[source]

A tuple of target qubits.

sympy.physics.quantum.gate.CNOT

alias of CNotGate

sympy.physics.quantum.gate.SWAP

alias of SwapGate

sympy.physics.quantum.gate.H

alias of HadamardGate

sympy.physics.quantum.gate.X

alias of XGate

sympy.physics.quantum.gate.Y

alias of YGate

sympy.physics.quantum.gate.Z

alias of ZGate

sympy.physics.quantum.gate.T

alias of TGate

sympy.physics.quantum.gate.S

alias of PhaseGate

sympy.physics.quantum.gate.Phase

alias of PhaseGate

sympy.physics.quantum.gate.normalized(normalize)[source]

Set flag controlling normalization of Hadamard gates by 1/sqrt(2).

This is a global setting that can be used to simplify the look of various expressions, by leaving off the leading 1/sqrt(2) of the Hadamard gate.

Parameters :

normalize : bool

Should the Hadamard gate include the 1/sqrt(2) normalization factor? When True, the Hadamard gate will have the 1/sqrt(2). When False, the Hadamard gate will not have this factor.

sympy.physics.quantum.gate.gate_sort(circuit)[source]

Sorts the gates while keeping track of commutation relations

This function uses a bubble sort to rearrange the order of gate application. Keeps track of Quantum computations special commutation relations (e.g. things that apply to the same Qubit do not commute with each other)

circuit is the Mul of gates that are to be sorted.

sympy.physics.quantum.gate.gate_simp(circuit)[source]

Simplifies gates symbolically

It first sorts gates using gate_sort. It then applies basic simplification rules to the circuit, e.g., XGate**2 = Identity

sympy.physics.quantum.gate.random_circuit(ngates, nqubits, gate_space=(<class 'sympy.physics.quantum.gate.XGate'>, <class 'sympy.physics.quantum.gate.YGate'>, <class 'sympy.physics.quantum.gate.ZGate'>, <class 'sympy.physics.quantum.gate.PhaseGate'>, <class 'sympy.physics.quantum.gate.TGate'>, <class 'sympy.physics.quantum.gate.HadamardGate'>, <class 'sympy.physics.quantum.gate.CNotGate'>, <class 'sympy.physics.quantum.gate.SwapGate'>))[source]

Return a random circuit of ngates and nqubits.

This uses an equally weighted sample of (X, Y, Z, S, T, H, CNOT, SWAP) gates.

Parameters :

ngates : int

The number of gates in the circuit.

nqubits : int

The number of qubits in the circuit.

gate_space : tuple

A tuple of the gate classes that will be used in the circuit. Repeating gate classes multiple times in this tuple will increase the frequency they appear in the random circuit.

Previous topic

Circuit Plot

Next topic

Grover’s Algorithm

This Page