Quantum Gates API reference for all quantum gate functions in pyqpanda3. Every gate function returns a QGate object that can be composed into QCircuit and QProg structures.
QGate Class QGate is the base type returned by all gate factory functions.
Signature Methods Method Signature Description qubits_numqubits_num() -> intNumber of qubits the gate acts on qubitsqubits() -> list[int]All qubit indices the gate acts on control_qubitscontrol_qubits() -> list[int]Control qubit indices target_qubitstarget_qubits() -> list[int]Target qubit indices daggerdagger() -> QGateReturn the dagger (conjugate transpose) of the gate is_daggeris_dagger() -> boolWhether the gate is in dagger form powerpower(k: float) -> QGateRaise the gate to a power clear_controlclear_control() -> NoneRemove all control qubits controlcontrol(qubit: int) -> QGateAdd a single control qubit, returning a controlled gate controlcontrol(qubits: list[int]) -> QGateAdd multiple control qubits, returning a controlled gate matrixmatrix(expanded: bool = False) -> numpy.ndarrayUnitary matrix representation; if expanded is True, expand to full system size gate_typegate_type() -> GateTypeEnum value identifying the gate type remapremap(qubit_map: dict[int, int]) -> QGateRemap qubit indices using a mapping dict remapremap(qubit_list: list[int]) -> QGateRemap qubit indices using an ordered list parametersparameters() -> list[float]Gate parameters set_parametersset_parameters(params: list[float]) -> NoneSet gate parameters namename() -> strGate name string
Single-Qubit Gates H — Hadamard python H ( qubit : int ) -> QGate Applies the Hadamard gate, creating equal superposition.
H = 1 2 [ 1 1 1 − 1 ] python from pyqpanda3 . core import H , CPUQVM , QProg , measure
qvm = CPUQVM ()
prog = QProg ()
prog << H ( 0 )
prog << measure ( 0 , 0 )
qvm . run ( prog , 1000 )
result = qvm . result ()
print ( result . get_counts ()) X — Pauli-X (NOT) python X ( qubit : int ) -> QGate Bit-flip (NOT) gate.
X = [ 0 1 1 0 ] Y — Pauli-Y python Y ( qubit : int ) -> QGate Y = [ 0 − i i 0 ] Z — Pauli-Z python Z ( qubit : int ) -> QGate Phase-flip gate.
Z = [ 1 0 0 − 1 ] I — Identity python I ( qubit : int ) -> QGate Identity gate (no operation).
I = [ 1 0 0 1 ] S — Phase Gate python S ( qubit : int ) -> QGate S = [ 1 0 0 i ] T — T Gate (π/8 Gate) python T ( qubit : int ) -> QGate T = [ 1 0 0 e i π / 4 ] X1 — SX Gate python X1 ( qubit : int ) -> QGate X 1 = 1 2 [ 1 − i − i 1 ] Y1 python Y1 ( qubit : int ) -> QGate Y 1 = 1 2 [ 1 − 1 1 1 ] Z1 python Z1 ( qubit : int ) -> QGate P — Phase Gate (Parameterized) python P ( qubit : int , theta : float ) -> QGate Parameter Type Description qubit intTarget qubit index theta floatRotation angle in radians
P ( θ ) = [ 1 0 0 e i θ ] RX — Rotation around X python RX ( qubit : int , theta : float ) -> QGate Parameter Type Description qubit intTarget qubit index theta floatRotation angle in radians
R X ( θ ) = e − i θ X / 2 = [ cos ( θ / 2 ) − i sin ( θ / 2 ) − i sin ( θ / 2 ) cos ( θ / 2 ) ] RY — Rotation around Y python RY ( qubit : int , theta : float ) -> QGate R Y ( θ ) = e − i θ Y / 2 = [ cos ( θ / 2 ) − sin ( θ / 2 ) sin ( θ / 2 ) cos ( θ / 2 ) ] RZ — Rotation around Z python RZ ( qubit : int , theta : float ) -> QGate R Z ( θ ) = e − i θ Z / 2 = [ e − i θ / 2 0 0 e i θ / 2 ] RPHI — R-Phi Gate python RPHI ( control : int , theta : float , phi : float ) -> QGate Note : RPhi is an alias for RPHI and functions identically.
Parameter Type Description control intTarget qubit index theta floatRotation angle phi floatPhase angle
U1 python U1 ( qubit : int , theta : float ) -> QGate Single-parameter gate. Equivalent to P ( θ ) .
U2 python U2 ( qubit : int , theta : float , phi : float ) -> QGate Parameter Type Description qubit intTarget qubit index theta floatFirst angle parameter phi floatSecond angle parameter
U3 python U3 ( qubit : int , theta : float , phi : float , lambda : float ) -> QGate Parameter Type Description qubit intTarget qubit index theta floatRotation angle phi floatFirst phase lambda floatSecond phase
U 3 ( θ , ϕ , λ ) = [ cos ( θ / 2 ) − e i λ sin ( θ / 2 ) e i ϕ sin ( θ / 2 ) e i ( ϕ + λ ) cos ( θ / 2 ) ] U4 python U4 ( qubit : int , theta : float , phi : float , lambda : float , gamma : float ) -> QGate Parameter Type Description qubit intTarget qubit index theta floatFirst angle phi floatSecond angle lambda floatThird angle gamma floatFourth angle
IDLE python IDLE ( qubit : int , nanoseconds : float ) -> QGate Parameter Type Description qubit intTarget qubit index nanoseconds floatDelay duration in nanoseconds
Two-Qubit Gates CNOT — Controlled-NOT python CNOT ( qubit1 : int , qubit2 : int ) -> QGate Parameter Type Description qubit1 intControl qubit index qubit2 intTarget qubit index
CNOT = [ 1 0 0 0 0 1 0 0 0 0 0 1 0 0 1 0 ] python from pyqpanda3 . core import H , CNOT , CPUQVM , QProg , measure
qvm = CPUQVM ()
prog = QProg ()
# Create Bell state
prog << H ( 0 ) << CNOT ( 0 , 1 )
prog << measure ( 0 , 0 ) << measure ( 1 , 1 )
qvm . run ( prog , 1000 )
print ( qvm . result (). get_counts ()) CZ — Controlled-Z python CZ ( qubit1 : int , qubit2 : int ) -> QGate CZ = [ 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 − 1 ] CP — Controlled-Phase python CP ( control : int , target : int , theta : float ) -> QGate Parameter Type Description control intControl qubit index target intTarget qubit index theta floatPhase angle in radians
CP ( θ ) = [ 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 e i θ ] CR — Controlled-Rotation (alias for CP) python CR ( control : int , target : int , theta : float ) -> QGate Equivalent to CP.
CRX — Controlled-RX python CRX ( control : int , target : int , theta : float ) -> QGate CRY — Controlled-RY python CRY ( control : int , target : int , theta : float ) -> QGate CRZ — Controlled-RZ python CRZ ( control : int , target : int , theta : float ) -> QGate SWAP python SWAP ( qubit1 : int , qubit2 : int ) -> QGate SWAP = [ 1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1 ] ISWAP python ISWAP ( qubit1 : int , qubit2 : int ) -> QGate iSWAP = [ 1 0 0 0 0 0 i 0 0 i 0 0 0 0 0 1 ] SQISWAP — Square-root iSWAP python SQISWAP ( qubit1 : int , qubit2 : int ) -> QGate ECHO python ECHO ( qubit : int , param : float = 0.0 ) -> QGate Parameter Type Description qubit intTarget qubit index param floatEcho parameter (default 0.0)
CU — Controlled-U python CU ( control : int , target : int , theta : float , phi : float , lambda : float , gamma : float ) -> QGate Parameter Type Description control intControl qubit index target intTarget qubit index theta floatRotation angle phi floatFirst phase lambda floatSecond phase gamma floatThird phase
RXX — Rotation around XX python RXX ( control : int , target : int , theta : float ) -> QGate R X X ( θ ) = e − i θ X ⊗ X / 2 RYY — Rotation around YY python RYY ( control : int , target : int , theta : float ) -> QGate R Y Y ( θ ) = e − i θ Y ⊗ Y / 2 RZZ — Rotation around ZZ python RZZ ( control : int , target : int , theta : float ) -> QGate R Z Z ( θ ) = e − i θ Z ⊗ Z / 2 RZX — Rotation around ZX python RZX ( control : int , target : int , theta : float ) -> QGate R Z X ( θ ) = e − i θ Z ⊗ X / 2 Multi-Qubit Gates TOFFOLI — CCX (Controlled-Controlled-NOT) python TOFFOLI ( qubit1 : int , qubit2 : int , qubit3 : int ) -> QGate Parameter Type Description qubit1 intFirst control qubit qubit2 intSecond control qubit qubit3 intTarget qubit
Flips the target qubit if and only if both control qubits are | 1 ⟩ .
Special Gates BARRIER python BARRIER ( qubits : list [ int ]) -> QGate Parameter Type Description qubits list[int]Qubit indices to apply barrier
Prevents optimization across the barrier during transpilation.
MS — Molmer-Sorensen Gate python MS ( qubit1 : int , qubit2 : int ) -> QGate Two-qubit entangling gate used in trapped-ion quantum computing.
Oracle python Oracle ( qubits : list [ int ], matrix : numpy . ndarray ) -> QGate Parameter Type Description qubits list[int]Qubit indices the oracle acts on matrix numpy.ndarrayComplex unitary matrix
Creates a custom gate from a given unitary matrix.
python import numpy as np
from pyqpanda3 . core import Oracle
# Create a 2-qubit oracle from a unitary matrix
mat = np . eye ( 4 , dtype = complex )
gate = Oracle ([ 0 , 1 ], mat ) Gate Operations All QGate objects support the following operations:
Dagger python gate = H ( 0 )
dg = gate . dagger () # Hermitian conjugate Control python gate = X ( 1 )
cgate = gate . control ([ 0 ]) # CNOT with qubit 0 as control Power python gate = S ( 0 )
t_gate = gate . power ( 2 ) # S^2 = Z See Also create_gate Creates a quantum gate by specifying its name, target qubits, and parameters. This is a generic factory function that constructs any supported gate type dynamically.
Signature python create_gate ( gate_name : str , qubits : list [ int ], params : list [ float ]) -> QGate Parameters Parameter Type Description gate_name strName of the gate to create (e.g., "H", "RX", "CNOT") qubits list[int]List of target qubit indices params list[float]List of parameter values (empty for non-parameterized gates)
Examples python from pyqpanda3 . core import create_gate
# Create a Hadamard gate on qubit 0
h = create_gate ( " H " , [ 0 ], [])
# Create an RX rotation gate on qubit 1 with angle 1.57
rx = create_gate ( " RX " , [ 1 ], [ 1.57 ])
# Create a CNOT gate with control=0, target=1
cnot = create_gate ( " CNOT " , [ 0 , 1 ], [])