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.CGate(*args, **kwargs)[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.
- property controls¶
A tuple of control qubits.
- property gate¶
The non-controlled gate that will be applied to the targets.
- property min_qubits¶
The minimum number of qubits this gate needs to act on.
- property nqubits¶
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.
- plot_gate(circ_plot, gate_idx)[source]¶
Plot the controlled gate. If simplify_cgate is true, simplify C-X and C-Z gates into their more familiar forms.
- property targets¶
A tuple of target qubits.
- class sympy.physics.quantum.gate.CGateS(*args, **kwargs)[source]¶
Version of CGate that allows gate simplifications. I.e. cnot looks like an oplus, cphase has dots, etc.
- class sympy.physics.quantum.gate.CNotGate(*args, **kwargs)[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>
- property controls¶
A tuple of control qubits.
- property gate¶
The non-controlled gate that will be applied to the targets.
- property min_qubits¶
The minimum number of qubits this gate needs to act on.
- property targets¶
A tuple of target qubits.
- class sympy.physics.quantum.gate.Gate(*args, **kwargs)[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 representation of the target part of the gate.
- Parameters:
format : str
The format string (‘sympy’,’numpy’, etc.)
- property min_qubits¶
The minimum number of qubits this gate needs to act on.
- property nqubits¶
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.
- property targets¶
A tuple of target qubits.
- sympy.physics.quantum.gate.H[source]¶
alias of
HadamardGate
- class sympy.physics.quantum.gate.HadamardGate(*args, **kwargs)[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.IdentityGate(*args, **kwargs)[source]¶
The single qubit identity gate.
- Parameters:
target : int
The target qubit this gate will apply to.
- class sympy.physics.quantum.gate.OneQubitGate(*args, **kwargs)[source]¶
A single qubit unitary gate base class.
- class sympy.physics.quantum.gate.PhaseGate(*args, **kwargs)[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(*args, **kwargs)[source]¶
Two qubit SWAP gate.
This gate swap the values of the two qubits.
- Parameters:
label : tuple
A tuple of the form (target1, target2).
- class sympy.physics.quantum.gate.TGate(*args, **kwargs)[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.TwoQubitGate(*args, **kwargs)[source]¶
A two qubit unitary gate base class.
- class sympy.physics.quantum.gate.UGate(*args, **kwargs)[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.)
- property targets¶
A tuple of target qubits.
- class sympy.physics.quantum.gate.XGate(*args, **kwargs)[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(*args, **kwargs)[source]¶
The single qubit Y gate.
- Parameters:
target : int
The target qubit this gate will apply to.
- class sympy.physics.quantum.gate.ZGate(*args, **kwargs)[source]¶
The single qubit Z gate.
- Parameters:
target : int
The target qubit this gate will apply to.
- 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.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.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.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.