Basic Quantum Gates

Basic quantum gates implement the standard single-qubit and multi-qubit quantum gate operations.

Overview

Basic quantum gates overview

Operator

Operation

Unitarity class

X_Bool

Pauli-X (bit flip)

SelfAdjoint

Y_Bool

Pauli-Y

SelfAdjoint

Z_Bool

Pauli-Z (phase flip)

SelfAdjoint

S_Bool

S gate (π/2 phase)

SelfAdjoint

T_Bool

T gate (π/4 phase)

SelfAdjoint

Phase_Bool

Arbitrary phase e^{iλ}

BaseOperator

RX_Bool

Rotation about the X axis

SelfAdjoint

RY_Bool

Rotation about the Y axis

SelfAdjoint

RZ_Bool

Rotation about the Z axis

SelfAdjoint

SX_Bool

√X gate

SelfAdjoint

U2_Bool

General single-qubit gate (2 parameters)

BaseOperator

U3_Bool

General single-qubit gate (3 parameters)

BaseOperator

Type Constraints

All quantum gates require:

  • Register type: Boolean (single-qubit gates)

  • Bit index: must be within the register size range [0, size)

# Correct: Boolean type for single-qubit gates
ps.System.add_register("qubit", ps.Boolean, 1)
ps.X_Bool("qubit", 0)(state)

# Wrong: bit index out of range
# ps.X_Bool("qubit", 1)(state)  # Raises an exception!

—

Pauli Gates

X_Bool (Pauli-X / NOT)

Operation: Bit flip |0⟩ ↔ |1⟩

Matrix:

\[\begin{split}X = \begin{pmatrix} 0 & 1 \\ 1 & 0 \end{pmatrix}\end{split}\]
import pysparq as ps

ps.System.clear()
ps.System.add_register("q", ps.Boolean, 1)

state = ps.SparseState()
# Initially |q=0⟩

ps.X_Bool("q", 0)(state)
# |q=1⟩

# Applying again restores the original state (self-adjoint)
ps.X_Bool("q", 0)(state)
# |q=0⟩

Y_Bool (Pauli-Y)

Matrix:

\[\begin{split}Y = \begin{pmatrix} 0 & -i \\ i & 0 \end{pmatrix}\end{split}\]
ps.Y_Bool("q", 0)(state)

Z_Bool (Pauli-Z)

Operation: Phase flip |1⟩ → -|1⟩

Matrix:

\[\begin{split}Z = \begin{pmatrix} 1 & 0 \\ 0 & -1 \end{pmatrix}\end{split}\]
ps.Z_Bool("q", 0)(state)

—

Phase Gates

S_Bool (S gate)

Operation: Phase rotation by π/2

Matrix:

\[\begin{split}S = \begin{pmatrix} 1 & 0 \\ 0 & i \end{pmatrix}\end{split}\]
ps.S_Bool("q", 0)(state)

T_Bool (T gate)

Operation: Phase rotation by π/4

Matrix:

\[\begin{split}T = \begin{pmatrix} 1 & 0 \\ 0 & e^{i\pi/4} \end{pmatrix}\end{split}\]
ps.T_Bool("q", 0)(state)

Phase_Bool (arbitrary phase)

Operation: Phase rotation e^{iλ}

Dagger: Phase rotation e^{-iλ}

# Phase rotation by π/3
op = ps.Phase_Bool("q", 0, np.pi / 3)
op(state)

# Undo
op.dag(state)

—

Rotation Gates

RX_Bool (X-axis rotation)

Matrix:

\[\begin{split}R_X(\theta) = \begin{pmatrix} \cos\frac{\theta}{2} & -i\sin\frac{\theta}{2} \\ -i\sin\frac{\theta}{2} & \cos\frac{\theta}{2} \end{pmatrix}\end{split}\]
import numpy as np

# X-axis rotation by π/2
ps.RX_Bool("q", np.pi / 2)(state)

RY_Bool (Y-axis rotation)

Matrix:

\[\begin{split}R_Y(\theta) = \begin{pmatrix} \cos\frac{\theta}{2} & -\sin\frac{\theta}{2} \\ \sin\frac{\theta}{2} & \cos\frac{\theta}{2} \end{pmatrix}\end{split}\]
ps.RY_Bool("q", np.pi / 2)(state)

RZ_Bool (Z-axis rotation)

Matrix:

\[\begin{split}R_Z(\theta) = \begin{pmatrix} e^{-i\theta/2} & 0 \\ 0 & e^{i\theta/2} \end{pmatrix}\end{split}\]
ps.RZ_Bool("q", np.pi / 2)(state)

SX_Bool (√X gate)

Operation: X^{1/2}

ps.SX_Bool("q", 0)(state)

—

Universal Gates

U2_Bool (2-parameter universal gate)

Matrix:

\[\begin{split}U_2(\phi, \lambda) = \frac{1}{\sqrt{2}} \begin{pmatrix} 1 & -e^{i\lambda} \\ e^{i\phi} & e^{i(\phi+\lambda)} \end{pmatrix}\end{split}\]

U3_Bool (3-parameter universal gate)

Matrix:

\[\begin{split}U_3(\theta, \phi, \lambda) = \begin{pmatrix} \cos\frac{\theta}{2} & -e^{i\lambda}\sin\frac{\theta}{2} \\ e^{i\phi}\sin\frac{\theta}{2} & e^{i(\phi+\lambda)}\cos\frac{\theta}{2} \end{pmatrix}\end{split}\]
import numpy as np

op = ps.U3_Bool("q", np.pi/4, np.pi/2, 0)
op(state)

# Undo
op.dag(state)

—

Other Gates

Rot_Bool (general rotation)

Operation: Apply an arbitrary 2×2 unitary matrix

import numpy as np

# Define a 2x2 matrix
matrix = np.array([
    [np.cos(np.pi/4), -np.sin(np.pi/4)],
    [np.sin(np.pi/4), np.cos(np.pi/4)]
])

ps.Rot_Bool("q", matrix)(state)

Reflection_Bool (reflection gate)

Operation: Reflection operation (the diffusion operator in Grover’s algorithm)


中文版 ===

基本量子门

基本量子门实现单量子比特和多量子比特的标准量子门操作。

概述

基本量子门总览

算子

操作

幺正类

X_Bool

Pauli-X(比特翻转)

SelfAdjoint

Y_Bool

Pauli-Y

SelfAdjoint

Z_Bool

Pauli-Z(相位翻转)

SelfAdjoint

S_Bool

S 门(π/2 相位)

SelfAdjoint

T_Bool

T 门(π/4 相位)

SelfAdjoint

Phase_Bool

任意相位 e^{iλ}

BaseOperator

RX_Bool

X 轴旋转

SelfAdjoint

RY_Bool

Y 轴旋转

SelfAdjoint

RZ_Bool

Z 轴旋转

SelfAdjoint

SX_Bool

√X 门

SelfAdjoint

U2_Bool

通用单量子比特门(2 参数)

BaseOperator

U3_Bool

通用单量子比特门(3 参数)

BaseOperator

类型约束

所有量子门要求:

  • 寄存器类型:``Boolean``(单量子比特门)

  • 位索引:必须在寄存器大小范围内 [0, size)

# 正确:Boolean 类型用于单量子比特门
ps.System.add_register("qubit", ps.Boolean, 1)
ps.X_Bool("qubit", 0)(state)

# 错误:位索引超出范围
# ps.X_Bool("qubit", 1)(state)  # 抛出异常!

—

Pauli 门

X_Bool(Pauli-X / NOT)

操作: 比特翻转 |0⟩ ↔ |1⟩

矩阵:

\[\begin{split}X = \begin{pmatrix} 0 & 1 \\ 1 & 0 \end{pmatrix}\end{split}\]
import pysparq as ps

ps.System.clear()
ps.System.add_register("q", ps.Boolean, 1)

state = ps.SparseState()
# 初始 |q=0⟩

ps.X_Bool("q", 0)(state)
# |q=1⟩

# 再次应用恢复原状态(自伴)
ps.X_Bool("q", 0)(state)
# |q=0⟩

Y_Bool(Pauli-Y)

矩阵:

\[\begin{split}Y = \begin{pmatrix} 0 & -i \\ i & 0 \end{pmatrix}\end{split}\]
ps.Y_Bool("q", 0)(state)

Z_Bool(Pauli-Z)

操作: 相位翻转 |1⟩ → -|1⟩

矩阵:

\[\begin{split}Z = \begin{pmatrix} 1 & 0 \\ 0 & -1 \end{pmatrix}\end{split}\]
ps.Z_Bool("q", 0)(state)

—

相位门

S_Bool(S 门)

操作: 相位旋转 π/2

矩阵:

\[\begin{split}S = \begin{pmatrix} 1 & 0 \\ 0 & i \end{pmatrix}\end{split}\]
ps.S_Bool("q", 0)(state)

T_Bool(T 门)

操作: 相位旋转 π/4

矩阵:

\[\begin{split}T = \begin{pmatrix} 1 & 0 \\ 0 & e^{i\pi/4} \end{pmatrix}\end{split}\]
ps.T_Bool("q", 0)(state)

Phase_Bool(任意相位)

操作: 相位旋转 e^{iλ}

Dagger: 相位旋转 e^{-iλ}

# 相位旋转 π/3
op = ps.Phase_Bool("q", 0, np.pi / 3)
op(state)

# 撤销
op.dag(state)

—

旋转门

RX_Bool(X 轴旋转)

矩阵:

\[\begin{split}R_X(\theta) = \begin{pmatrix} \cos\frac{\theta}{2} & -i\sin\frac{\theta}{2} \\ -i\sin\frac{\theta}{2} & \cos\frac{\theta}{2} \end{pmatrix}\end{split}\]
import numpy as np

# X 轴旋转 π/2
ps.RX_Bool("q", np.pi / 2)(state)

RY_Bool(Y 轴旋转)

矩阵:

\[\begin{split}R_Y(\theta) = \begin{pmatrix} \cos\frac{\theta}{2} & -\sin\frac{\theta}{2} \\ \sin\frac{\theta}{2} & \cos\frac{\theta}{2} \end{pmatrix}\end{split}\]
ps.RY_Bool("q", np.pi / 2)(state)

RZ_Bool(Z 轴旋转)

矩阵:

\[\begin{split}R_Z(\theta) = \begin{pmatrix} e^{-i\theta/2} & 0 \\ 0 & e^{i\theta/2} \end{pmatrix}\end{split}\]
ps.RZ_Bool("q", np.pi / 2)(state)

SX_Bool(√X 门)

操作: X^{1/2}

ps.SX_Bool("q", 0)(state)

—

通用门

U2_Bool(2 参数通用门)

矩阵:

\[\begin{split}U_2(\phi, \lambda) = \frac{1}{\sqrt{2}} \begin{pmatrix} 1 & -e^{i\lambda} \\ e^{i\phi} & e^{i(\phi+\lambda)} \end{pmatrix}\end{split}\]

U3_Bool(3 参数通用门)

矩阵:

\[\begin{split}U_3(\theta, \phi, \lambda) = \begin{pmatrix} \cos\frac{\theta}{2} & -e^{i\lambda}\sin\frac{\theta}{2} \\ e^{i\phi}\sin\frac{\theta}{2} & e^{i(\phi+\lambda)}\cos\frac{\theta}{2} \end{pmatrix}\end{split}\]
import numpy as np

op = ps.U3_Bool("q", np.pi/4, np.pi/2, 0)
op(state)

# 撤销
op.dag(state)

—

其他门

Rot_Bool(通用旋转)

操作: 应用任意 2×2 幺正矩阵

import numpy as np

# 定义 2x2 矩阵
matrix = np.array([
    [np.cos(np.pi/4), -np.sin(np.pi/4)],
    [np.sin(np.pi/4), np.cos(np.pi/4)]
])

ps.Rot_Bool("q", matrix)(state)

Reflection_Bool(反射门)

操作: 反射操作(Grover 算法中的扩散算子)