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[111−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())

运行结果:

{'0': 515, '1': 485}

X — Pauli-X (NOT) ​

python
X(qubit: int) -> QGate

Bit-flip (NOT) gate.

X=[0110]

Y — Pauli-Y ​

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

Z — Pauli-Z ​

python
Z(qubit: int) -> QGate

Phase-flip gate.

Z=[100−1]

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[1−i−i1]

Y1 ​

python
Y1(qubit: int) -> QGate
Y1=12[1−111]

Z1 ​

python
Z1(qubit: int) -> QGate
Z1=12[1−i001+i]

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(θ)=e−iθX/2=[cos⁡(θ/2)−isin⁡(θ/2)−isin⁡(θ/2)cos⁡(θ/2)]

RY — Rotation around Y ​

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

RZ — Rotation around Z ​

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

RPHI — R-Phi Gate ​

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

Note: RPhi is a variational gate (VQGate) with a different signature. It is not the same as RPHI. See Variational Gate for details.

ParameterTypeDescription
controlintTarget qubit index
thetafloatRotation angle
phifloatPhase angle
RPHI(θ,ϕ)=[cos⁡(θ/2)−ie−iϕsin⁡(θ/2)−ieiϕsin⁡(θ/2)cos⁡(θ/2)]

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
U2(θ,ϕ)=12[1−eiϕeiθei(θ+ϕ)]

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
U4(θ,ϕ,λ,γ)=[ei(θ−ϕ/2−γ/2)cos⁡(λ/2)−ei(θ−ϕ/2+γ/2)sin⁡(λ/2)ei(θ+ϕ/2−γ/2)sin⁡(λ/2)ei(θ+ϕ/2+γ/2)cos⁡(λ/2)]

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())

运行结果:

{'00': 479, '11': 521}

CZ — Controlled-Z ​

python
CZ(qubit1: int, qubit2: int) -> QGate
CZ=[100001000010000−1]

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
SQISWAP=[1000012i200i21200001]

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(θ)=e−iθX⊗X/2=[cos⁡(θ/2)00−isin⁡(θ/2)0cos⁡(θ/2)−isin⁡(θ/2)00−isin⁡(θ/2)cos⁡(θ/2)0−isin⁡(θ/2)00cos⁡(θ/2)]

RYY — Rotation around YY ​

python
RYY(control: int, target: int, theta: float) -> QGate
RYY(θ)=e−iθY⊗Y/2=[cos⁡(θ/2)00isin⁡(θ/2)0cos⁡(θ/2)−isin⁡(θ/2)00−isin⁡(θ/2)cos⁡(θ/2)0isin⁡(θ/2)00cos⁡(θ/2)]

RZZ — Rotation around ZZ ​

python
RZZ(control: int, target: int, theta: float) -> QGate
RZZ(θ)=e−iθZ⊗Z/2=[e−iθ/20000eiθ/20000eiθ/20000e−iθ/2]

RZX — Rotation around ZX ​

python
RZX(control: int, target: int, theta: float) -> QGate
RZX(θ)=e−iθZ⊗X/2=[cos⁡(θ/2)0−isin⁡(θ/2)00cos⁡(θ/2)0isin⁡(θ/2)−isin⁡(θ/2)0cos⁡(θ/2)00isin⁡(θ/2)0cos⁡(θ/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.

RESET ​

python
RESET(qubit: int) -> QGate
ParameterTypeDescription
qubitintQubit index to reset to `

New in 0.5.0. Applies the native, non-unitary RESET channel: the qubit is measured and conditionally flipped, collapsing it to |0> with certainty while leaving the classical registers untouched. Supported on the CPU state-vector and density-matrix simulators; other backends raise at execution time, and controlled RESET has no physical definition. The operation round-trips through OriginIR and is parsed from OpenQASM 2.0 reset q[i]; statements; exporting it back to OpenQASM raises RuntimeError (use OriginIR instead).

python
from pyqpanda3.core import CPUQVM, QProg, RESET, X, measure

prog = QProg() << X(0) << RESET(0) << measure(0, 0)
qvm = CPUQVM()
qvm.run(prog, 100)
print(qvm.result().get_counts())

Output:

text
{'0': 100}

MS — Molmer-Sorensen Gate ​

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

Two-qubit entangling gate used in trapped-ion quantum computing.

MS=12[100−i01−i00−i10−i001]

Oracle ​

python
Oracle(qubits: list[int], matrix: numpy.ndarray, check_unitary: bool = True) -> QGate
ParameterTypeDescription
qubitslist[int]Qubit indices the oracle acts on
matrixnumpy.ndarrayComplex unitary matrix
check_unitaryboolValidate that matrix is unitary before constructing the gate (new in 0.5.0). Default True.

Creates a custom gate from a given unitary matrix.

The matrix is applied in the order the qubits are given: qubits[0] is the most significant factor of the matrix, matching the left-most term of the Kronecker product. On qubits [0, 1], X ⊗ I therefore flips qubit 0 while I ⊗ X flips qubit 1, and listing the qubits as [1, 0] swaps which qubit each factor acts on.

python
import numpy as np
from pyqpanda3.core import Oracle, CPUQVM, QProg

x = np.array([[0, 1], [1, 0]], dtype=complex)
i = np.eye(2, dtype=complex)

# qubits[0] is the left-most factor, so X acts on qubit 0
qvm = CPUQVM()
qvm.run(QProg() << Oracle([0, 1], np.kron(x, i)), 1)
print({format(k, '02b'): round(abs(v), 3)
       for k, v in enumerate(qvm.result().get_state_vector()) if abs(v) > 1e-9})

Output:

text
{'10': 1.0}

check_unitary rejects a matrix that is not unitary, which would define a non-physical gate. Leave it enabled unless the matrix comes from a trusted source and the validation is a measurable cost:

python
import numpy as np
from pyqpanda3.core import Oracle

not_unitary = np.array([[1, 1], [0, 1]], dtype=complex)

try:
    Oracle([0], not_unitary)
except ValueError as e:
    print(type(e).__name__, "-", e)

gate = Oracle([0], not_unitary, check_unitary=False)
print("built without validation:", type(gate).__name__)

Output:

text
ValueError - Non-unitary matrix for QOracle-gate.
built without validation: QGate

Gate Operations ​

All QGate objects support the following operations:

Dagger ​

python
from pyqpanda3.core import H

gate = H(0)
dg = gate.dagger()  # Hermitian conjugate

Control ​

python
from pyqpanda3.core import X

gate = X(1)
cgate = gate.control([0])  # CNOT with qubit 0 as control

Power ​

python
from pyqpanda3.core import S

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.