Skip to content

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

python
class QGate

Methods

MethodSignatureDescription
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=12[1111]
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=[0110]

Y — Pauli-Y

python
Y(qubit: int) -> QGate
Y=[0ii0]

Z — Pauli-Z

python
Z(qubit: int) -> QGate

Phase-flip gate.

Z=[1001]

I — Identity

python
I(qubit: int) -> QGate

Identity gate (no operation).

I=[1001]

S — Phase Gate

python
S(qubit: int) -> QGate
S=[100i]

T — T Gate (π/8 Gate)

python
T(qubit: int) -> QGate
T=[100eiπ/4]

X1 — SX Gate

python
X1(qubit: int) -> QGate
X1=12[1ii1]

Y1

python
Y1(qubit: int) -> QGate
Y1=12[1111]

Z1

python
Z1(qubit: int) -> QGate

P — Phase Gate (Parameterized)

python
P(qubit: int, theta: float) -> QGate
ParameterTypeDescription
qubitintTarget qubit index
thetafloatRotation angle in radians
P(θ)=[100eiθ]

RX — Rotation around X

python
RX(qubit: int, theta: float) -> QGate
ParameterTypeDescription
qubitintTarget qubit index
thetafloatRotation angle in radians
RX(θ)=eiθX/2=[cos(θ/2)isin(θ/2)isin(θ/2)cos(θ/2)]

RY — Rotation around Y

python
RY(qubit: int, theta: float) -> QGate
RY(θ)=eiθY/2=[cos(θ/2)sin(θ/2)sin(θ/2)cos(θ/2)]

RZ — Rotation around Z

python
RZ(qubit: int, theta: float) -> QGate
RZ(θ)=eiθZ/2=[eiθ/200eiθ/2]

RPHI — R-Phi Gate

python
RPHI(control: int, theta: float, phi: float) -> QGate

Note: RPhi is an alias for RPHI and functions identically.

ParameterTypeDescription
controlintTarget qubit index
thetafloatRotation angle
phifloatPhase 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
ParameterTypeDescription
qubitintTarget qubit index
thetafloatFirst angle parameter
phifloatSecond angle parameter

U3

python
U3(qubit: int, theta: float, phi: float, lambda: float) -> QGate
ParameterTypeDescription
qubitintTarget qubit index
thetafloatRotation angle
phifloatFirst phase
lambdafloatSecond phase
U3(θ,ϕ,λ)=[cos(θ/2)eiλsin(θ/2)eiϕsin(θ/2)ei(ϕ+λ)cos(θ/2)]

U4

python
U4(qubit: int, theta: float, phi: float, lambda: float, gamma: float) -> QGate
ParameterTypeDescription
qubitintTarget qubit index
thetafloatFirst angle
phifloatSecond angle
lambdafloatThird angle
gammafloatFourth angle

IDLE

python
IDLE(qubit: int, nanoseconds: float) -> QGate
ParameterTypeDescription
qubitintTarget qubit index
nanosecondsfloatDelay duration in nanoseconds

Two-Qubit Gates

CNOT — Controlled-NOT

python
CNOT(qubit1: int, qubit2: int) -> QGate
ParameterTypeDescription
qubit1intControl qubit index
qubit2intTarget qubit index
CNOT=[1000010000010010]
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=[1000010000100001]

CP — Controlled-Phase

python
CP(control: int, target: int, theta: float) -> QGate
ParameterTypeDescription
controlintControl qubit index
targetintTarget qubit index
thetafloatPhase angle in radians
CP(θ)=[100001000010000eiθ]

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=[1000001001000001]

ISWAP

python
ISWAP(qubit1: int, qubit2: int) -> QGate
iSWAP=[100000i00i000001]

SQISWAP — Square-root iSWAP

python
SQISWAP(qubit1: int, qubit2: int) -> QGate

ECHO

python
ECHO(qubit: int, param: float = 0.0) -> QGate
ParameterTypeDescription
qubitintTarget qubit index
paramfloatEcho parameter (default 0.0)

CU — Controlled-U

python
CU(control: int, target: int, theta: float, phi: float, lambda: float, gamma: float) -> QGate
ParameterTypeDescription
controlintControl qubit index
targetintTarget qubit index
thetafloatRotation angle
phifloatFirst phase
lambdafloatSecond phase
gammafloatThird phase

RXX — Rotation around XX

python
RXX(control: int, target: int, theta: float) -> QGate
RXX(θ)=eiθXX/2

RYY — Rotation around YY

python
RYY(control: int, target: int, theta: float) -> QGate
RYY(θ)=eiθYY/2

RZZ — Rotation around ZZ

python
RZZ(control: int, target: int, theta: float) -> QGate
RZZ(θ)=eiθZZ/2

RZX — Rotation around ZX

python
RZX(control: int, target: int, theta: float) -> QGate
RZX(θ)=eiθZX/2

Multi-Qubit Gates

TOFFOLI — CCX (Controlled-Controlled-NOT)

python
TOFFOLI(qubit1: int, qubit2: int, qubit3: int) -> QGate
ParameterTypeDescription
qubit1intFirst control qubit
qubit2intSecond control qubit
qubit3intTarget qubit

Flips the target qubit if and only if both control qubits are |1.

Special Gates

BARRIER

python
BARRIER(qubits: list[int]) -> QGate
ParameterTypeDescription
qubitslist[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
ParameterTypeDescription
qubitslist[int]Qubit indices the oracle acts on
matrixnumpy.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

ParameterTypeDescription
gate_namestrName of the gate to create (e.g., "H", "RX", "CNOT")
qubitslist[int]List of target qubit indices
paramslist[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], [])

Released under the MIT License.