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
class QGateMethods
| Method | Signature | Description |
|---|---|---|
qubits_num | qubits_num() -> int | Number of qubits the gate acts on |
qubits | qubits() -> list[int] | All qubit indices the gate acts on |
control_qubits | control_qubits() -> list[int] | Control qubit indices |
target_qubits | target_qubits() -> list[int] | Target qubit indices |
dagger | dagger() -> QGate | Return the dagger (conjugate transpose) of the gate |
is_dagger | is_dagger() -> bool | Whether the gate is in dagger form |
power | power(k: float) -> QGate | Raise the gate to a power |
clear_control | clear_control() -> None | Remove all control qubits |
control | control(qubit: int) -> QGate | Add a single control qubit, returning a controlled gate |
control | control(qubits: list[int]) -> QGate | Add multiple control qubits, returning a controlled gate |
matrix | matrix(expanded: bool = False) -> numpy.ndarray | Unitary matrix representation; if expanded is True, expand to full system size |
gate_type | gate_type() -> GateType | Enum value identifying the gate type |
remap | remap(qubit_map: dict[int, int]) -> QGate | Remap qubit indices using a mapping dict |
remap | remap(qubit_list: list[int]) -> QGate | Remap qubit indices using an ordered list |
parameters | parameters() -> list[float] | Gate parameters |
set_parameters | set_parameters(params: list[float]) -> None | Set gate parameters |
name | name() -> str | Gate name string |
Single-Qubit Gates
H — Hadamard
H(qubit: int) -> QGateApplies the Hadamard gate, creating equal superposition.
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)
X(qubit: int) -> QGateBit-flip (NOT) gate.
Y — Pauli-Y
Y(qubit: int) -> QGateZ — Pauli-Z
Z(qubit: int) -> QGatePhase-flip gate.
I — Identity
I(qubit: int) -> QGateIdentity gate (no operation).
S — Phase Gate
S(qubit: int) -> QGateT — T Gate (π/8 Gate)
T(qubit: int) -> QGateX1 — SX Gate
X1(qubit: int) -> QGateY1
Y1(qubit: int) -> QGateZ1
Z1(qubit: int) -> QGateP — Phase Gate (Parameterized)
P(qubit: int, theta: float) -> QGate| Parameter | Type | Description |
|---|---|---|
| qubit | int | Target qubit index |
| theta | float | Rotation angle in radians |
RX — Rotation around X
RX(qubit: int, theta: float) -> QGate| Parameter | Type | Description |
|---|---|---|
| qubit | int | Target qubit index |
| theta | float | Rotation angle in radians |
RY — Rotation around Y
RY(qubit: int, theta: float) -> QGateRZ — Rotation around Z
RZ(qubit: int, theta: float) -> QGateRPHI — R-Phi Gate
RPHI(control: int, theta: float, phi: float) -> QGateNote:
RPhiis a variational gate (VQGate) with a different signature. It is not the same asRPHI. See Variational Gate for details.
| Parameter | Type | Description |
|---|---|---|
| control | int | Target qubit index |
| theta | float | Rotation angle |
| phi | float | Phase angle |
U1
U1(qubit: int, theta: float) -> QGateSingle-parameter gate. Equivalent to
U2
U2(qubit: int, theta: float, phi: float) -> QGate| Parameter | Type | Description |
|---|---|---|
| qubit | int | Target qubit index |
| theta | float | First angle parameter |
| phi | float | Second angle parameter |
U3
U3(qubit: int, theta: float, phi: float, lambda: float) -> QGate| Parameter | Type | Description |
|---|---|---|
| qubit | int | Target qubit index |
| theta | float | Rotation angle |
| phi | float | First phase |
| lambda | float | Second phase |
U4
U4(qubit: int, theta: float, phi: float, lambda: float, gamma: float) -> QGate| Parameter | Type | Description |
|---|---|---|
| qubit | int | Target qubit index |
| theta | float | First angle |
| phi | float | Second angle |
| lambda | float | Third angle |
| gamma | float | Fourth angle |
IDLE
IDLE(qubit: int, nanoseconds: float) -> QGate| Parameter | Type | Description |
|---|---|---|
| qubit | int | Target qubit index |
| nanoseconds | float | Delay duration in nanoseconds |
Two-Qubit Gates
CNOT — Controlled-NOT
CNOT(qubit1: int, qubit2: int) -> QGate| Parameter | Type | Description |
|---|---|---|
| qubit1 | int | Control qubit index |
| qubit2 | int | Target qubit index |
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
CZ(qubit1: int, qubit2: int) -> QGateCP — Controlled-Phase
CP(control: int, target: int, theta: float) -> QGate| Parameter | Type | Description |
|---|---|---|
| control | int | Control qubit index |
| target | int | Target qubit index |
| theta | float | Phase angle in radians |
CR — Controlled-Rotation (alias for CP)
CR(control: int, target: int, theta: float) -> QGateEquivalent to CP.
CRX — Controlled-RX
CRX(control: int, target: int, theta: float) -> QGateCRY — Controlled-RY
CRY(control: int, target: int, theta: float) -> QGateCRZ — Controlled-RZ
CRZ(control: int, target: int, theta: float) -> QGateSWAP
SWAP(qubit1: int, qubit2: int) -> QGateISWAP
ISWAP(qubit1: int, qubit2: int) -> QGateSQISWAP — Square-root iSWAP
SQISWAP(qubit1: int, qubit2: int) -> QGateECHO
ECHO(qubit: int, param: float = 0.0) -> QGate| Parameter | Type | Description |
|---|---|---|
| qubit | int | Target qubit index |
| param | float | Echo parameter (default 0.0) |
CU — Controlled-U
CU(control: int, target: int, theta: float, phi: float, lambda: float, gamma: float) -> QGate| Parameter | Type | Description |
|---|---|---|
| control | int | Control qubit index |
| target | int | Target qubit index |
| theta | float | Rotation angle |
| phi | float | First phase |
| lambda | float | Second phase |
| gamma | float | Third phase |
RXX — Rotation around XX
RXX(control: int, target: int, theta: float) -> QGateRYY — Rotation around YY
RYY(control: int, target: int, theta: float) -> QGateRZZ — Rotation around ZZ
RZZ(control: int, target: int, theta: float) -> QGateRZX — Rotation around ZX
RZX(control: int, target: int, theta: float) -> QGateMulti-Qubit Gates
TOFFOLI — CCX (Controlled-Controlled-NOT)
TOFFOLI(qubit1: int, qubit2: int, qubit3: int) -> QGate| Parameter | Type | Description |
|---|---|---|
| qubit1 | int | First control qubit |
| qubit2 | int | Second control qubit |
| qubit3 | int | Target qubit |
Flips the target qubit if and only if both control qubits are
Special Gates
BARRIER
BARRIER(qubits: list[int]) -> QGate| Parameter | Type | Description |
|---|---|---|
| qubits | list[int] | Qubit indices to apply barrier |
Prevents optimization across the barrier during transpilation.
RESET
RESET(qubit: int) -> QGate| Parameter | Type | Description |
|---|---|---|
| qubit | int | Qubit 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).
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:
{'0': 100}MS — Molmer-Sorensen Gate
MS(qubit1: int, qubit2: int) -> QGateTwo-qubit entangling gate used in trapped-ion quantum computing.
Oracle
Oracle(qubits: list[int], matrix: numpy.ndarray, check_unitary: bool = True) -> QGate| Parameter | Type | Description |
|---|---|---|
| qubits | list[int] | Qubit indices the oracle acts on |
| matrix | numpy.ndarray | Complex unitary matrix |
| check_unitary | bool | Validate 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.
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:
{'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:
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:
ValueError - Non-unitary matrix for QOracle-gate.
built without validation: QGateGate Operations
All QGate objects support the following operations:
Dagger
from pyqpanda3.core import H
gate = H(0)
dg = gate.dagger() # Hermitian conjugateControl
from pyqpanda3.core import X
gate = X(1)
cgate = gate.control([0]) # CNOT with qubit 0 as controlPower
from pyqpanda3.core import S
gate = S(0)
t_gate = gate.power(2) # S^2 = ZSee Also
- Circuit — QCircuit and QProg for composing gates
- Enums — GateType enumeration
- Variational Gate — Parameterized gate variants
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
create_gate(gate_name: str, qubits: list[int], params: list[float]) -> QGateParameters
| Parameter | Type | Description |
|---|---|---|
| gate_name | str | Name 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
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], [])