quairkit.database.matrix

Gate matrices.

quairkit.database.matrix.phase(dim)

Generate phase operator for qudit

Parameters:
dim : int

dimension of qudit

Returns:

Phase operator for qudit

Return type:

Tensor

Examples

dim = 2
phase_operator = phase(dim)
print(f'The phase_operator is:\n{phase_operator}')
The phase_operator is:
tensor([[ 1.+0.0000e+00j,  0.+0.0000e+00j],
        [ 0.+0.0000e+00j, -1.+1.2246e-16j]])
quairkit.database.matrix.shift(dim)

Generate shift operator for qudit

Parameters:
dim : int

dimension of qudit

Returns:

Shift operator for qudit

Return type:

Tensor

Examples

dim = 2
shift_operator = shift(dim)
print(f'The shift_operator is:\n{shift_operator}')
The shift_operator is:
tensor([[0.+0.j, 1.+0.j],
        [1.+0.j, 0.+0.j]])
quairkit.database.matrix.grover_matrix(oracle, dtype=None)

Construct the Grover operator based on oracle.

Parameters:
oracle : ndarray | Tensor

the input oracle \(A\) to be rotated.

Returns:

Grover operator in form

\[G = A (2 |0^n \rangle\langle 0^n| - I^n) A^\dagger \cdot (I - 2|1 \rangle\langle 1|) \otimes I^{n-1}\]

Return type:

ndarray | Tensor

Examples

oracle = torch.tensor([[0, 1], [1, 0]], dtype=torch.cfloat)
grover_op = grover_matrix(oracle)
print(f'The grover_matrix is:\n{grover_op}')
The grover_matrix is:
tensor([[-1.+0.j,  0.+0.j],
        [ 0.+0.j, -1.+0.j]])
quairkit.database.matrix.qft_matrix(num_qubits)

Construct the quantum Fourier transpose (QFT) gate.

Parameters:
num_qubits : int

number of qubits \(n\) st. \(N = 2^n\).

Returns:

A matrix in the form

\[\begin{split}QFT = \frac{1}{\sqrt{N}} \begin{bmatrix} 1 & 1 & \cdots & 1 \\ 1 & \omega_N & \cdots & \omega_N^{N-1} \\ \vdots & \vdots & \ddots & \vdots \\ 1 & \omega_N^{N-1} & \cdots & \omega_N^{(N-1)^2} \end{bmatrix}, \quad \omega_N = \exp\left(\frac{2\pi i}{N}\right).\end{split}\]

Return type:

Tensor

Examples

num_qubits = 1
qft_gate = qft_matrix(num_qubits)
print(f'The QFT gate is:\n{qft_gate}')
The QFT gate is:
tensor([[ 0.7071+0.0000e+00j,  0.7071+0.0000e+00j],
        [ 0.7071+0.0000e+00j, -0.7071+8.6596e-17j]])
quairkit.database.matrix.h()

Generate the matrix of the Hadamard gate.

\[\begin{split}H = \frac{1}{\sqrt{2}} \begin{bmatrix} 1 & 1 \\ 1 & -1 \end{bmatrix}\end{split}\]
Returns:

The matrix of the H gate.

Return type:

Tensor

Examples

H = h()
print(f'The Hadamard gate is:\n{H}')
The Hadamard gate is:
tensor([[ 0.7071+0.j,  0.7071+0.j],
        [ 0.7071+0.j, -0.7071+0.j]])
quairkit.database.matrix.s()

Generate the matrix of the S gate.

\[\begin{split}S = \begin{bmatrix} 1 & 0 \\ 0 & i \end{bmatrix}\end{split}\]
Returns:

The matrix of the S gate.

Return type:

Tensor

Examples

S = s()
print(f'The S gate is:\n{S}')
The S gate is:
tensor([[1.+0.j, 0.+0.j],
        [0.+0.j, 0.+1.j]])
quairkit.database.matrix.sdg()

Generate the matrix of the Sdg (S-dagger) gate.

\[\begin{split}S^\dagger = \begin{bmatrix} 1 & 0 \\ 0 & -i \end{bmatrix}\end{split}\]
Returns:

The matrix of the Sdg gate.

Return type:

Tensor

Examples

Sdg = sdg()
print(f'The dagger of S gate is:\n{Sdg}')
The dagger of S gate is:
tensor([[1.+0.j,  0.+0.j],
        [0.+0.j, -0.-1.j]])
quairkit.database.matrix.t()

Generate the matrix of the T gate.

\[\begin{split}T = \begin{bmatrix} 1 & 0 \\ 0 & e^{i\pi/4} \end{bmatrix}\end{split}\]
Returns:

The matrix of the T gate.

Return type:

Tensor

Examples

T = t()
print(f'The T gate is:\n{T}')
The T gate is:
tensor([[1.0000+0.0000j, 0.0000+0.0000j],
        [0.0000+0.0000j, 0.7071+0.7071j]])
quairkit.database.matrix.tdg()

Generate the matrix of the Tdg (T-dagger) gate.

\[\begin{split}T^\dagger = \begin{bmatrix} 1 & 0 \\ 0 & e^{-i\pi/4} \end{bmatrix}\end{split}\]
Returns:

The matrix of the Tdg gate.

Return type:

Tensor

Examples

Tdg = tdg()
print(f'The dagger of T gate is:\n{Tdg}')
The dagger of T gate is:
tensor([[1.0000+0.0000j, 0.0000+0.0000j],
        [0.0000+0.0000j, 0.7071-0.7071j]])
quairkit.database.matrix.eye(dim=2)

Generate the identity matrix.

\[\begin{split}I = \begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix}\end{split}\]
Parameters:
dim : int

the dimension of the identity matrix (default is 2 for a qubit).

Returns:

The identity matrix.

Return type:

Tensor

Examples

I = eye()
print(f'The Identity Matrix is:\n{I}')
The Identity Matrix is:
tensor([[1.+0.j, 0.+0.j],
        [0.+0.j, 1.+0.j]])
quairkit.database.matrix.x()

Generate the Pauli X matrix.

\[\begin{split}X = \begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix}\end{split}\]
Returns:

The matrix of the X gate.

Return type:

Tensor

Examples

X = x()
print(f'The Pauli X Matrix is:\n{X}')
The Pauli X Matrix is:
tensor([[0.+0.j, 1.+0.j],
        [1.+0.j, 0.+0.j]])
quairkit.database.matrix.y()

Generate the Pauli Y matrix.

\[\begin{split}Y = \begin{bmatrix} 0 & -i \\ i & 0 \end{bmatrix}\end{split}\]
Returns:

The matrix of the Y gate.

Return type:

Tensor

Examples

Y = y()
print(f'The Pauli Y Matrix is:\n{Y}')
The Pauli Y Matrix is:
tensor([[0.+0.j, -0.-1.j],
        [0.+1.j,  0.+0.j]])
quairkit.database.matrix.z()

Generate the Pauli Z matrix.

\[\begin{split}Z = \begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix}\end{split}\]
Returns:

The matrix of the Z gate.

Return type:

Tensor

Examples

Z = z()
print(f'The Pauli Z Matrix is:\n{Z}')
The Pauli Z Matrix is:
tensor([[ 1.+0.j,  0.+0.j],
        [ 0.+0.j, -1.+0.j]])
quairkit.database.matrix.p(theta)

Generate the P gate matrix.

\[\begin{split}P(\theta) = \begin{bmatrix} 1 & 0 \\ 0 & e^{i\theta} \end{bmatrix}\end{split}\]
Parameters:
theta : ndarray | Tensor | Iterable[float] | float

the (batched) parameter, with shape [1].

Returns:

The matrix of the P gate.

Return type:

ndarray | Tensor

Examples

theta = torch.tensor([torch.pi / 4])
p_matrix = p(theta)
print(f'The P Gate is:\n{p_matrix}')
The P Gate is:
tensor([[1.0000+0.0000j, 0.0000+0.0000j],
        [0.0000+0.0000j, 0.7071+0.7071j]])
quairkit.database.matrix.rx(theta)

Generate the RX gate matrix.

\[\begin{split}R_X(\theta) = \begin{bmatrix} \cos\frac{\theta}{2} & -i\sin\frac{\theta}{2} \\ -i\sin\frac{\theta}{2} & \cos\frac{\theta}{2} \end{bmatrix}\end{split}\]
Parameters:
theta : ndarray | Tensor | Iterable[float] | float

the (batched) parameter, with shape [1].

Returns:

The matrix of the RX gate.

Return type:

ndarray | Tensor

Examples

theta = torch.tensor([torch.pi / 4])
R_x = rx(theta)
print(f'The R_x Gate is:\n{R_x}')
The R_x Gate is:
tensor([[0.9239+0.0000j, 0.0000-0.3827j],
        [0.0000-0.3827j, 0.9239+0.0000j]])
quairkit.database.matrix.ry(theta)

Generate the RY gate matrix.

\[\begin{split}R_Y(\theta) = \begin{bmatrix} \cos\frac{\theta}{2} & -\sin\frac{\theta}{2} \\ \sin\frac{\theta}{2} & \cos\frac{\theta}{2} \end{bmatrix}\end{split}\]
Parameters:
theta : ndarray | Tensor | Iterable[float] | float

the (batched) parameter, with shape [1].

Returns:

The matrix of the RY gate.

Return type:

ndarray | Tensor

Examples

theta = torch.tensor([torch.pi / 4])
R_y = ry(theta)
print(f'The R_y Gate is:\n{R_y}')
The R_y Gate is:
tensor([[ 0.9239+0.j, -0.3827+0.j],
        [ 0.3827+0.j,  0.9239+0.j]])
quairkit.database.matrix.rz(theta)

Generate the RZ gate matrix.

\[\begin{split}R_Z(\theta) = \begin{bmatrix} e^{-i\theta/2} & 0 \\ 0 & e^{i\theta/2} \end{bmatrix}\end{split}\]
Parameters:
theta : ndarray | Tensor | Iterable[float] | float

the (batched) parameter, with shape [1].

Returns:

The matrix of the RZ gate.

Return type:

ndarray | Tensor

Examples

theta = torch.tensor([torch.pi / 4])
R_z = rz(theta)
print(f'The R_z Gate is:\n{R_z}')
The R_z Gate is:
tensor([[0.9239-0.3827j, 0.0000+0.0000j],
        [0.0000+0.0000j, 0.9239+0.3827j]])
quairkit.database.matrix.u3(theta)

Generate the U3 gate matrix.

\[\begin{split}U_3(\theta, \phi, \lambda) = \begin{bmatrix} \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{bmatrix}\end{split}\]
Parameters:
theta : ndarray | Tensor | Iterable[float]

the (batched) parameters, with shape [3, 1].

Returns:

The matrix of the U3 gate.

Return type:

ndarray | Tensor

Examples

theta = torch.tensor([[torch.pi / 4], [torch.pi / 3], [torch.pi / 6]])
u3_matrix = u3(theta)
print(f'The U3 Gate is:\n{u3_matrix}')
The U3 Gate is:
tensor([[ 9.2388e-01+0.0000j, -3.3141e-01-0.1913j],
        [ 1.9134e-01+0.3314j, -4.0384e-08+0.9239j]])
quairkit.database.matrix.cnot()

Generate the CNOT gate matrix.

\[\begin{split}CNOT = |0\rangle \langle 0| \otimes I + |1\rangle \langle 1| \otimes X = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0 \end{bmatrix}\end{split}\]
Returns:

The matrix of the CNOT gate.

Return type:

Tensor

Examples

CNOT = cnot()
print(f'The CNOT Gate is:\n{CNOT}')
The CNOT Gate is:
tensor([[1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
        [0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j],
        [0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j],
        [0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j]])
quairkit.database.matrix.cy()

Generate the CY gate matrix.

\[\begin{split}CY = |0\rangle\langle0| \otimes I + |1\rangle\langle1| \otimes Y = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & -i \\ 0 & 0 & i & 0 \end{bmatrix}\end{split}\]
Returns:

The matrix of the CY gate.

Return type:

Tensor

Examples

CY = cy()
print(f'The CY Gate is:\n{CY}')
The CY Gate is:
tensor([[1.+0.j,  0.+0.j,  0.+0.j,  0.+0.j],
        [0.+0.j,  1.+0.j,  0.+0.j,  0.+0.j],
        [0.+0.j,  0.+0.j,  0.+0.j, -0.-1.j],
        [0.+0.j,  0.+0.j,  0.+1.j,  0.+0.j]])
quairkit.database.matrix.cz()

Generate the CZ gate matrix.

\[\begin{split}CZ = |0\rangle\langle0| \otimes I + |1\rangle\langle1| \otimes Z = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & -1 \end{bmatrix}\end{split}\]
Returns:

The matrix of the CZ gate.

Return type:

Tensor

Examples

CZ = cz()
print(f'The CZ Gate is:\n{CZ}')
The CZ Gate is:
tensor([[ 1.+0.j,  0.+0.j,  0.+0.j,  0.+0.j],
        [ 0.+0.j,  1.+0.j,  0.+0.j,  0.+0.j],
        [ 0.+0.j,  0.+0.j,  1.+0.j,  0.+0.j],
        [ 0.+0.j,  0.+0.j,  0.+0.j, -1.+0.j]])
quairkit.database.matrix.swap()

Generate the SWAP gate matrix.

\[\begin{split}SWAP = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}\end{split}\]
Returns:

The matrix of the SWAP gate.

Return type:

Tensor

Examples

SWAP = swap()
print(f'The SWAP Gate is:\n{SWAP}')
The SWAP Gate is:
tensor([[1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
        [0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j],
        [0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j],
        [0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j]])
quairkit.database.matrix.cp(theta)

Generate the CP gate matrix.

\[\begin{split}CP(\theta) = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & e^{i\theta} \end{bmatrix}\end{split}\]
Parameters:
theta : ndarray | Tensor | Iterable[float] | float

the (batched) parameter, with shape [1].

Returns:

The matrix of the CP gate.

Return type:

ndarray | Tensor

Examples

theta = torch.tensor([torch.pi / 4])
CP = cp(theta)
print(f'The CP Gate is:\n{CP}')
The CP Gate is:
tensor([[1.0000+0.0000j, 0.0000+0.0000j, 0.0000+0.0000j, 0.0000+0.0000j],
        [0.0000+0.0000j, 1.0000+0.0000j, 0.0000+0.0000j, 0.0000+0.0000j],
        [0.0000+0.0000j, 0.0000+0.0000j, 1.0000+0.0000j, 0.0000+0.0000j],
        [0.0000+0.0000j, 0.0000+0.0000j, 0.0000+0.0000j, 0.7071+0.7071j]])
quairkit.database.matrix.crx(theta)

Generate the CR_X gate matrix.

\[\begin{split}CR_X = |0\rangle\langle0| \otimes I + |1\rangle\langle1| \otimes R_X = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & \cos\frac{\theta}{2} & -i\sin\frac{\theta}{2} \\ 0 & 0 & -i\sin\frac{\theta}{2} & \cos\frac{\theta}{2} \end{bmatrix}\end{split}\]
Parameters:
theta : ndarray | Tensor | Iterable[float] | float

the (batched) parameter, with shape [1].

Returns:

The matrix of the CR_X gate.

Return type:

ndarray | Tensor

Examples

theta = torch.tensor([torch.pi / 4])
CR_X = crx(theta)
print(f'The CR_X Gate is:\n{CR_X}')
The CR_X Gate is:
tensor([[1.0000+0.0000j, 0.+0.0000j, 0.+0.0000j, 0.+0.0000j],
        [0.+0.0000j, 1.0000+0.0000j, 0.+0.0000j, 0.+0.0000j],
        [0.+0.0000j, 0.+0.0000j, 0.9239+0.0000j, 0.+-0.3827j],
        [0.+0.0000j, 0.+0.0000j, 0.+-0.3827j, 0.9239+0.0000j]])
quairkit.database.matrix.cry(theta)

Generate the CR_Y gate matrix.

\[\begin{split}CR_Y = |0\rangle\langle0| \otimes I + |1\rangle\langle1| \otimes R_Y = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & \cos\frac{\theta}{2} & -\sin\frac{\theta}{2} \\ 0 & 0 & \sin\frac{\theta}{2} & \cos\frac{\theta}{2} \end{bmatrix}\end{split}\]
Parameters:
theta : ndarray | Tensor | Iterable[float] | float

the (batched) parameter, with shape [1].

Returns:

The matrix of the CR_Y gate.

Return type:

ndarray | Tensor

Examples

theta = torch.tensor([torch.pi / 4])
CR_Y = cry(theta)
print(f'The CR_Y Gate is:\n{CR_Y}')
The CR_Y Gate is:
tensor([[1.0000+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
        [0.+0.j, 1.0000+0.j, 0.+0.j, 0.+0.j],
        [0.+0.j, 0.+0.j, 0.9239+0.j, -0.3827+0.j],
        [0.+0.j, 0.+0.j, 0.3827+0.j, 0.9239+0.j]])
quairkit.database.matrix.crz(theta)

Generate the CR_Z gate matrix.

\[\begin{split}CR_Z = |0\rangle\langle0| \otimes I + |1\rangle\langle1| \otimes R_Z = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & e^{-i\theta/2} & 0 \\ 0 & 0 & 0 & e^{i\theta/2} \end{bmatrix}\end{split}\]
Parameters:
theta : ndarray | Tensor | Iterable[float] | float

the (batched) parameter, with shape [1].

Returns:

The matrix of the CR_Z gate.

Return type:

ndarray | Tensor

Examples

theta = torch.tensor([torch.pi / 4])
CR_Z = crz(theta)
print(f'The CR_Z Gate is:\n{CR_Z}')
The CR_Z Gate is:
tensor([[1.0000+0.0000j, 0.+0.0000j, 0.+0.0000j, 0.+0.0000j],
        [0.+0.0000j, 1.0000+0.0000j, 0.+0.0000j, 0.+0.0000j],
        [0.+0.0000j, 0.+0.0000j, 0.9239-0.3827j, 0.+0.0000j],
        [0.+0.0000j, 0.+0.0000j, 0.+0.0000j, 0.9239+0.3827j]])
quairkit.database.matrix.cu(theta)

Generate the CU gate matrix.

\[\begin{split}CU = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & e^{i\gamma}\cos\frac{\theta}{2} & -e^{i(\lambda+\gamma)}\sin\frac{\theta}{2} \\ 0 & 0 & e^{i(\phi+\gamma)}\sin\frac{\theta}{2} & e^{i(\phi+\lambda+\gamma)}\cos\frac{\theta}{2} \end{bmatrix}\end{split}\]
Parameters:
theta : ndarray | Tensor | Iterable[float]

the (batched) parameters, with shape [4, 1].

Returns:

The matrix of the CU gate.

Return type:

ndarray | Tensor

Examples

theta = torch.tensor([[torch.pi / 4], [torch.pi / 3], [torch.pi / 6], [torch.pi / 6]])
CU = cu(theta)
print(f'The CU Gate is:\n{CU}')
The CU Gate is:
tensor([[1.0000e+00+0.0000j, 0.0000e+00+0.0000j, 0.0000e+00+0.0000j, 0.0000e+00+0.0000j],
        [0.0000e+00+0.0000j, 1.0000e+00+0.0000j, 0.0000e+00+0.0000j, 0.0000e+00+0.0000j],
        [0.0000e+00+0.0000j, 0.0000e+00+0.0000j, 8.0010e-01+0.4619j, -1.9134e-01-0.3314j],
        [0.0000e+00+0.0000j, 0.0000e+00+0.0000j, -1.6728e-08+0.3827j, -4.6194e-01+0.8001j]])
quairkit.database.matrix.rxx(theta)

Generate the RXX gate matrix.

\[\begin{split}R_{XX}(\theta) = \begin{bmatrix} \cos\frac{\theta}{2} & 0 & 0 & -i\sin\frac{\theta}{2} \\ 0 & \cos\frac{\theta}{2} & -i\sin\frac{\theta}{2} & 0 \\ 0 & -i\sin\frac{\theta}{2} & \cos\frac{\theta}{2} & 0 \\ -i\sin\frac{\theta}{2} & 0 & 0 & \cos\frac{\theta}{2} \end{bmatrix}\end{split}\]
Parameters:
theta : ndarray | Tensor | Iterable[float] | float

the (batched) parameter, with shape [1].

Returns:

The matrix of the RXX gate.

Return type:

ndarray | Tensor

Examples

theta = torch.tensor([torch.pi / 4])
R_XX = rxx(theta)
print(f'The R_XX Gate is:\n{R_XX}')
The R_XX Gate is:
tensor([[0.9239+0.0000j, 0.0000+0.0000j, 0.0000+0.0000j, 0.0000-0.3827j],
        [0.0000+0.0000j, 0.9239+0.0000j, 0.0000-0.3827j, 0.0000+0.0000j],
        [0.0000+0.0000j, 0.0000-0.3827j, 0.9239+0.0000j, 0.0000+0.0000j],
        [0.0000-0.3827j, 0.0000+0.0000j, 0.0000+0.0000j, 0.9239+0.0000j]])
quairkit.database.matrix.ryy(theta)

Generate the RYY gate matrix.

\[\begin{split}R_{YY}(\theta) = \begin{bmatrix} \cos\frac{\theta}{2} & 0 & 0 & i\sin\frac{\theta}{2} \\ 0 & \cos\frac{\theta}{2} & -i\sin\frac{\theta}{2} & 0 \\ 0 & -i\sin\frac{\theta}{2} & \cos\frac{\theta}{2} & 0 \\ i\sin\frac{\theta}{2} & 0 & 0 & \cos\frac{\theta}{2} \end{bmatrix}\end{split}\]
Parameters:
theta : ndarray | Tensor | Iterable[float] | float

the (batched) parameter, with shape [1].

Returns:

The matrix of the RYY gate.

Return type:

ndarray | Tensor

Examples

theta = torch.tensor([torch.pi / 4])
R_YY = ryy(theta)
print(f'The R_YY Gate is:\n{R_YY}')
The R_YY Gate is:
tensor([[0.9239+0.0000j, 0.0000+0.0000j, 0.0000+0.0000j, 0.0000+0.3827j],
        [0.0000+0.0000j, 0.9239+0.0000j, 0.0000-0.3827j, 0.0000+0.0000j],
        [0.0000+0.0000j, 0.0000-0.3827j, 0.9239+0.0000j, 0.0000+0.0000j],
        [0.0000+0.3827j, 0.0000+0.0000j, 0.0000+0.0000j, 0.9239+0.0000j]])
quairkit.database.matrix.rzz(theta)

Generate the RZZ gate matrix.

\[\begin{split}R_{ZZ}(\theta) = \begin{bmatrix} e^{-i\theta/2} & 0 & 0 & 0 \\ 0 & e^{i\theta/2} & 0 & 0 \\ 0 & 0 & e^{i\theta/2} & 0 \\ 0 & 0 & 0 & e^{-i\theta/2} \end{bmatrix}\end{split}\]
Parameters:
theta : ndarray | Tensor | Iterable[float] | float

the (batched) parameter, with shape [1].

Returns:

The matrix of the RZZ gate.

Return type:

ndarray | Tensor

Examples

theta = torch.tensor([torch.pi / 4])
R_ZZ = rzz(theta)
print(f'The R_ZZ Gate is:\n{R_ZZ}')
The R_ZZ Gate is:
tensor([[0.9239-0.3827j, 0.0000+0.0000j, 0.0000+0.0000j, 0.0000+0.0000j],
        [0.0000+0.0000j, 0.9239+0.3827j, 0.0000+0.0000j, 0.0000+0.0000j],
        [0.0000+0.0000j, 0.0000+0.0000j, 0.9239+0.3827j, 0.0000+0.0000j],
        [0.0000+0.0000j, 0.0000+0.0000j, 0.0000+0.0000j, 0.9239-0.3827j]])
quairkit.database.matrix.ms()

Generate the MS gate matrix.

\[\begin{split}MS = R_{XX}\left(-\frac{\pi}{2}\right) = \frac{1}{\sqrt{2}} \begin{bmatrix} 1 & 0 & 0 & i \\ 0 & 1 & i & 0 \\ 0 & i & 1 & 0 \\ i & 0 & 0 & 1 \end{bmatrix}\end{split}\]
Returns:

The matrix of the MS gate.

Return type:

Tensor

Examples

MS = ms()
print(f'The MS Gate is:\n{MS}')
The MS Gate is:
tensor([[0.7071+0.0000j, 0.0000+0.0000j, 0.0000+0.0000j, 0.0000+0.7071j],
        [0.0000+0.0000j, 0.7071+0.0000j, 0.0000+0.7071j, 0.0000+0.0000j],
        [0.0000+0.0000j, 0.0000+0.7071j, 0.7071+0.0000j, 0.0000+0.0000j],
        [0.0000+0.7071j, 0.0000+0.0000j, 0.0000+0.0000j, 0.7071+0.0000j]])
quairkit.database.matrix.cswap()

Generate the CSWAP gate matrix.

\[\begin{split}CSWAP = \begin{bmatrix} 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 \end{bmatrix}\end{split}\]
Returns:

The matrix of the CSWAP gate.

Return type:

Tensor

Examples

CSWAP = cswap()
print(f'The CSWAP Gate is:\n{CSWAP}')
The CSWAP Gate is:
tensor([[1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
        [0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
        [0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
        [0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
        [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
        [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j],
        [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j],
        [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j]])
quairkit.database.matrix.toffoli()

Generate the Toffoli gate matrix.

\[\begin{split}Toffoli = \begin{bmatrix} 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 \\ 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 \end{bmatrix}\end{split}\]
Returns:

The matrix of the Toffoli gate.

Return type:

Tensor

Examples

Toffoli = toffoli()
print(f'The Toffoli Gate is:\n{Toffoli}')
The Toffoli Gate is:
tensor([[1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
        [0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
        [0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
        [0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
        [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
        [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j],
        [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j],
        [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j]])
quairkit.database.matrix.universal2(theta)

Generate the universal two-qubit gate matrix.

Parameters:
theta : ndarray | Tensor | Iterable[float]

the (batched) parameter with shape [15].

Returns:

The matrix of the universal two-qubit gate.

Return type:

ndarray | Tensor

Examples

theta = torch.tensor([
    0.5, 1.0, 1.5, 2.0, 2.5,
    3.0, 3.5, 4.0, 4.5, 5.0,
    5.5, 6.0, 6.5, 7.0, 7.5])
Universal2 = universal2(theta)
print(f'The matrix of universal two qubits gate is:\n{Universal2}')
The matrix of universal two qubits gate is:
tensor([[-0.2858-0.0270j,  0.4003+0.3090j, -0.6062+0.0791j,  0.5359+0.0323j],
        [-0.0894-0.1008j, -0.5804+0.0194j,  0.3156+0.1677j,  0.7090-0.1194j],
        [-0.8151-0.2697j,  0.2345-0.1841j,  0.3835-0.1154j, -0.0720+0.0918j],
        [-0.2431+0.3212j, -0.1714+0.5374j,  0.1140+0.5703j, -0.2703+0.3289j]])
quairkit.database.matrix.universal3(theta)

Generate the universal three-qubit gate matrix.

Parameters:
theta : ndarray | Tensor | Iterable[float]

the (batched) parameter with shape [81].

Returns:

The matrix of the universal three-qubit gate.

Return type:

ndarray | Tensor

Examples

theta = torch.tensor([
    0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0,
    1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0,
    2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 3.0,
    3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 4.0,
    4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 4.9, 5.0,
    5.1, 5.2, 5.3, 5.4, 5.5, 5.6, 5.7, 5.8, 5.9, 6.0,
    6.1, 6.2, 6.3, 6.4, 6.5, 6.6, 6.7, 6.8, 6.9, 7.0,
    7.1, 7.2, 7.3, 7.4, 7.5, 7.6, 7.7, 7.8, 7.9, 8.0,
    8.1])
Universal3 = universal3(theta)
print(f'The matrix of universal three qubits gate is:\n{Universal3}')
The matrix of universal three qubits gate is:
tensor([[-0.0675-0.0941j, -0.4602+0.0332j,  0.2635+0.0044j,  0.0825+0.3465j,
         -0.0874-0.3635j, -0.1177-0.4195j, -0.2735+0.3619j, -0.1760-0.1052j],
        [ 0.0486+0.0651j, -0.1123+0.0494j,  0.1903+0.0057j, -0.2080+0.2926j,
         -0.2099+0.0630j, -0.1406+0.5173j, -0.1431-0.3538j, -0.5460-0.1847j],
        [ 0.0827-0.0303j,  0.1155+0.1111j,  0.5391-0.0701j, -0.4229-0.2655j,
         -0.1546+0.1943j, -0.0455+0.1744j, -0.3242+0.3539j,  0.3118-0.0041j],
        [-0.1222+0.3984j,  0.1647-0.1817j,  0.3294-0.1486j, -0.0293-0.1503j,
          0.0100-0.6481j,  0.2424+0.1575j,  0.2485+0.0232j, -0.1053+0.1873j],
        [-0.4309-0.0791j, -0.2071-0.0482j, -0.4331+0.0866j, -0.5454-0.1778j,
         -0.1401-0.0230j,  0.0170+0.0299j,  0.0078+0.2231j, -0.2324+0.3369j],
        [ 0.0330+0.3056j,  0.2612+0.6464j, -0.2138-0.1748j, -0.2322-0.0598j,
          0.1387-0.1573j,  0.0914-0.2963j, -0.2712-0.1351j, -0.1272-0.1940j],
        [ 0.0449-0.3844j,  0.1135+0.2846j, -0.0251+0.3854j,  0.0442-0.0149j,
         -0.3671-0.1774j,  0.5158+0.1148j,  0.2151+0.1433j, -0.0188-0.3040j],
        [-0.4124-0.4385j,  0.2306+0.0894j,  0.0104-0.2180j, -0.0180+0.2869j,
         -0.1030-0.2991j, -0.1473+0.0931j, -0.1686-0.3451j,  0.3825+0.1480j]])
quairkit.database.matrix.universal_qudit(theta, dimension)

Generate a universal gate matrix for qudits using a generalized Gell-Mann basis.

Parameters:
theta : ndarray | Tensor | Iterable[float]

the (batched) parameter with shape [dimension**2 - 1].

dimension : int

the dimension of the qudit.

Returns:

The matrix of the d-dimensional unitary gate.

Return type:

ndarray | Tensor

Examples

dimension = 2
theta = torch.linspace(0.1, 2.5, dimension**2 - 1)
u_qudit_matrix = universal_qudit(theta, dimension)
print(f'The matrix of 2-dimensional unitary gate is:\n{u_qudit_matrix}')
The matrix of 2-dimensional unitary gate is:
tensor([[-0.9486+0.2806j,  0.1459+0.0112j],
        [-0.1459+0.0112j, -0.9486-0.2806j]])
quairkit.database.matrix.Uf(f, n)

Construct the unitary matrix used in Simon’s algorithm.

Parameters:
f : Callable[[int], int]

a 2-to-1 or 1-to-1 function \(f: \{0, 1\}^n \to \{0, 1\}^n\).

n : int

length of the bit string.

Returns:

A 2n-qubit unitary matrix satisfying

\[U|x, y\rangle = |x, y \oplus f(x)\rangle\]

Return type:

Tensor

Examples

def f(x: int) -> int:
    return x

unitary_matrix = Uf(f, 1)
print(f'Unitary matrix is:\n{unitary_matrix}')
Unitary matrix is:
tensor([[1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
        [0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j],
        [0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j],
        [0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j]])
quairkit.database.matrix.Of(f, n)

Construct the oracle unitary matrix for an unstructured search problem.

Parameters:
f : Callable[[int], int]

a function \(f: \{0, 1\}^n \to \{0, 1\}\).

n : int

length of the bit string.

Returns:

An n-qubit unitary matrix satisfying

\[U|x\rangle = (-1)^{f(x)}|x\rangle\]

Return type:

Tensor

Examples

def f(x: int) -> int:
    return x

unitary_matrix = Of(f, 1)
print(f'Unitary matrix is:\n{unitary_matrix}')
Unitary matrix is:
tensor([[ 1.+0.j,  0.+0.j],
        [ 0.+0.j, -1.+0.j]])
quairkit.database.matrix.permutation_matrix(perm, system_dim=2)

Construct a unitary matrix representing a permutation operation on a quantum system.

Parameters:
perm : List[int]

A list representing the permutation of subsystems. For example, [1, 0, 2] swaps the first two subsystems.

system_dim : int | List[int]

The dimension of each subsystem. - If an integer, all subsystems are assumed to have the same dimension. - If a list, it specifies the dimension of each subsystem individually.

Returns:

A unitary matrix representing the permutation in the Hilbert space.

Examples

perm = [1, 0]
U_perm = permutation_matrix(perm, system_dim=2)
print(f'The permutation matrix is:\n{U_perm}')
The permutation matrix is:
tensor([[0.+0.j, 1.+0.j],
        [1.+0.j, 0.+0.j]])
quairkit.database.matrix.phase_gate(dim)

Generate phase operator for qudit

Parameters:
dim : int

dimension of qudit

Returns:

Phase operator for qudit

Return type:

Tensor

Examples

dim = 2
phase_operator = phase(dim)
print(f'The phase_operator is:\n{phase_operator}')
The phase_operator is:
tensor([[ 1.+0.0000e+00j,  0.+0.0000e+00j],
        [ 0.+0.0000e+00j, -1.+1.2246e-16j]])
quairkit.database.matrix.shift_gate(dim)

Generate shift operator for qudit

Parameters:
dim : int

dimension of qudit

Returns:

Shift operator for qudit

Return type:

Tensor

Examples

dim = 2
shift_operator = shift(dim)
print(f'The shift_operator is:\n{shift_operator}')
The shift_operator is:
tensor([[0.+0.j, 1.+0.j],
        [1.+0.j, 0.+0.j]])
quairkit.database.matrix.h_gate()

Generate the matrix of the Hadamard gate.

\[\begin{split}H = \frac{1}{\sqrt{2}} \begin{bmatrix} 1 & 1 \\ 1 & -1 \end{bmatrix}\end{split}\]
Returns:

The matrix of the H gate.

Return type:

Tensor

Examples

H = h()
print(f'The Hadamard gate is:\n{H}')
The Hadamard gate is:
tensor([[ 0.7071+0.j,  0.7071+0.j],
        [ 0.7071+0.j, -0.7071+0.j]])
quairkit.database.matrix.s_gate()

Generate the matrix of the S gate.

\[\begin{split}S = \begin{bmatrix} 1 & 0 \\ 0 & i \end{bmatrix}\end{split}\]
Returns:

The matrix of the S gate.

Return type:

Tensor

Examples

S = s()
print(f'The S gate is:\n{S}')
The S gate is:
tensor([[1.+0.j, 0.+0.j],
        [0.+0.j, 0.+1.j]])
quairkit.database.matrix.sdg_gate()

Generate the matrix of the Sdg (S-dagger) gate.

\[\begin{split}S^\dagger = \begin{bmatrix} 1 & 0 \\ 0 & -i \end{bmatrix}\end{split}\]
Returns:

The matrix of the Sdg gate.

Return type:

Tensor

Examples

Sdg = sdg()
print(f'The dagger of S gate is:\n{Sdg}')
The dagger of S gate is:
tensor([[1.+0.j,  0.+0.j],
        [0.+0.j, -0.-1.j]])
quairkit.database.matrix.t_gate()

Generate the matrix of the T gate.

\[\begin{split}T = \begin{bmatrix} 1 & 0 \\ 0 & e^{i\pi/4} \end{bmatrix}\end{split}\]
Returns:

The matrix of the T gate.

Return type:

Tensor

Examples

T = t()
print(f'The T gate is:\n{T}')
The T gate is:
tensor([[1.0000+0.0000j, 0.0000+0.0000j],
        [0.0000+0.0000j, 0.7071+0.7071j]])
quairkit.database.matrix.tdg_gate()

Generate the matrix of the Tdg (T-dagger) gate.

\[\begin{split}T^\dagger = \begin{bmatrix} 1 & 0 \\ 0 & e^{-i\pi/4} \end{bmatrix}\end{split}\]
Returns:

The matrix of the Tdg gate.

Return type:

Tensor

Examples

Tdg = tdg()
print(f'The dagger of T gate is:\n{Tdg}')
The dagger of T gate is:
tensor([[1.0000+0.0000j, 0.0000+0.0000j],
        [0.0000+0.0000j, 0.7071-0.7071j]])
quairkit.database.matrix.eye_gate(dim=2)

Generate the identity matrix.

\[\begin{split}I = \begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix}\end{split}\]
Parameters:
dim : int

the dimension of the identity matrix (default is 2 for a qubit).

Returns:

The identity matrix.

Return type:

Tensor

Examples

I = eye()
print(f'The Identity Matrix is:\n{I}')
The Identity Matrix is:
tensor([[1.+0.j, 0.+0.j],
        [0.+0.j, 1.+0.j]])
quairkit.database.matrix.x_gate()

Generate the Pauli X matrix.

\[\begin{split}X = \begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix}\end{split}\]
Returns:

The matrix of the X gate.

Return type:

Tensor

Examples

X = x()
print(f'The Pauli X Matrix is:\n{X}')
The Pauli X Matrix is:
tensor([[0.+0.j, 1.+0.j],
        [1.+0.j, 0.+0.j]])
quairkit.database.matrix.y_gate()

Generate the Pauli Y matrix.

\[\begin{split}Y = \begin{bmatrix} 0 & -i \\ i & 0 \end{bmatrix}\end{split}\]
Returns:

The matrix of the Y gate.

Return type:

Tensor

Examples

Y = y()
print(f'The Pauli Y Matrix is:\n{Y}')
The Pauli Y Matrix is:
tensor([[0.+0.j, -0.-1.j],
        [0.+1.j,  0.+0.j]])
quairkit.database.matrix.z_gate()

Generate the Pauli Z matrix.

\[\begin{split}Z = \begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix}\end{split}\]
Returns:

The matrix of the Z gate.

Return type:

Tensor

Examples

Z = z()
print(f'The Pauli Z Matrix is:\n{Z}')
The Pauli Z Matrix is:
tensor([[ 1.+0.j,  0.+0.j],
        [ 0.+0.j, -1.+0.j]])
quairkit.database.matrix.p_gate(theta)

Generate the P gate matrix.

\[\begin{split}P(\theta) = \begin{bmatrix} 1 & 0 \\ 0 & e^{i\theta} \end{bmatrix}\end{split}\]
Parameters:
theta : ndarray | Tensor | Iterable[float] | float

the (batched) parameter, with shape [1].

Returns:

The matrix of the P gate.

Return type:

ndarray | Tensor

Examples

theta = torch.tensor([torch.pi / 4])
p_matrix = p(theta)
print(f'The P Gate is:\n{p_matrix}')
The P Gate is:
tensor([[1.0000+0.0000j, 0.0000+0.0000j],
        [0.0000+0.0000j, 0.7071+0.7071j]])
quairkit.database.matrix.rx_gate(theta)

Generate the RX gate matrix.

\[\begin{split}R_X(\theta) = \begin{bmatrix} \cos\frac{\theta}{2} & -i\sin\frac{\theta}{2} \\ -i\sin\frac{\theta}{2} & \cos\frac{\theta}{2} \end{bmatrix}\end{split}\]
Parameters:
theta : ndarray | Tensor | Iterable[float] | float

the (batched) parameter, with shape [1].

Returns:

The matrix of the RX gate.

Return type:

ndarray | Tensor

Examples

theta = torch.tensor([torch.pi / 4])
R_x = rx(theta)
print(f'The R_x Gate is:\n{R_x}')
The R_x Gate is:
tensor([[0.9239+0.0000j, 0.0000-0.3827j],
        [0.0000-0.3827j, 0.9239+0.0000j]])
quairkit.database.matrix.ry_gate(theta)

Generate the RY gate matrix.

\[\begin{split}R_Y(\theta) = \begin{bmatrix} \cos\frac{\theta}{2} & -\sin\frac{\theta}{2} \\ \sin\frac{\theta}{2} & \cos\frac{\theta}{2} \end{bmatrix}\end{split}\]
Parameters:
theta : ndarray | Tensor | Iterable[float] | float

the (batched) parameter, with shape [1].

Returns:

The matrix of the RY gate.

Return type:

ndarray | Tensor

Examples

theta = torch.tensor([torch.pi / 4])
R_y = ry(theta)
print(f'The R_y Gate is:\n{R_y}')
The R_y Gate is:
tensor([[ 0.9239+0.j, -0.3827+0.j],
        [ 0.3827+0.j,  0.9239+0.j]])
quairkit.database.matrix.rz_gate(theta)

Generate the RZ gate matrix.

\[\begin{split}R_Z(\theta) = \begin{bmatrix} e^{-i\theta/2} & 0 \\ 0 & e^{i\theta/2} \end{bmatrix}\end{split}\]
Parameters:
theta : ndarray | Tensor | Iterable[float] | float

the (batched) parameter, with shape [1].

Returns:

The matrix of the RZ gate.

Return type:

ndarray | Tensor

Examples

theta = torch.tensor([torch.pi / 4])
R_z = rz(theta)
print(f'The R_z Gate is:\n{R_z}')
The R_z Gate is:
tensor([[0.9239-0.3827j, 0.0000+0.0000j],
        [0.0000+0.0000j, 0.9239+0.3827j]])
quairkit.database.matrix.u3_gate(theta)

Generate the U3 gate matrix.

\[\begin{split}U_3(\theta, \phi, \lambda) = \begin{bmatrix} \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{bmatrix}\end{split}\]
Parameters:
theta : ndarray | Tensor | Iterable[float]

the (batched) parameters, with shape [3, 1].

Returns:

The matrix of the U3 gate.

Return type:

ndarray | Tensor

Examples

theta = torch.tensor([[torch.pi / 4], [torch.pi / 3], [torch.pi / 6]])
u3_matrix = u3(theta)
print(f'The U3 Gate is:\n{u3_matrix}')
The U3 Gate is:
tensor([[ 9.2388e-01+0.0000j, -3.3141e-01-0.1913j],
        [ 1.9134e-01+0.3314j, -4.0384e-08+0.9239j]])
quairkit.database.matrix.cnot_gate()

Generate the CNOT gate matrix.

\[\begin{split}CNOT = |0\rangle \langle 0| \otimes I + |1\rangle \langle 1| \otimes X = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0 \end{bmatrix}\end{split}\]
Returns:

The matrix of the CNOT gate.

Return type:

Tensor

Examples

CNOT = cnot()
print(f'The CNOT Gate is:\n{CNOT}')
The CNOT Gate is:
tensor([[1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
        [0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j],
        [0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j],
        [0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j]])
quairkit.database.matrix.cy_gate()

Generate the CY gate matrix.

\[\begin{split}CY = |0\rangle\langle0| \otimes I + |1\rangle\langle1| \otimes Y = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & -i \\ 0 & 0 & i & 0 \end{bmatrix}\end{split}\]
Returns:

The matrix of the CY gate.

Return type:

Tensor

Examples

CY = cy()
print(f'The CY Gate is:\n{CY}')
The CY Gate is:
tensor([[1.+0.j,  0.+0.j,  0.+0.j,  0.+0.j],
        [0.+0.j,  1.+0.j,  0.+0.j,  0.+0.j],
        [0.+0.j,  0.+0.j,  0.+0.j, -0.-1.j],
        [0.+0.j,  0.+0.j,  0.+1.j,  0.+0.j]])
quairkit.database.matrix.cz_gate()

Generate the CZ gate matrix.

\[\begin{split}CZ = |0\rangle\langle0| \otimes I + |1\rangle\langle1| \otimes Z = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & -1 \end{bmatrix}\end{split}\]
Returns:

The matrix of the CZ gate.

Return type:

Tensor

Examples

CZ = cz()
print(f'The CZ Gate is:\n{CZ}')
The CZ Gate is:
tensor([[ 1.+0.j,  0.+0.j,  0.+0.j,  0.+0.j],
        [ 0.+0.j,  1.+0.j,  0.+0.j,  0.+0.j],
        [ 0.+0.j,  0.+0.j,  1.+0.j,  0.+0.j],
        [ 0.+0.j,  0.+0.j,  0.+0.j, -1.+0.j]])
quairkit.database.matrix.swap_gate()

Generate the SWAP gate matrix.

\[\begin{split}SWAP = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}\end{split}\]
Returns:

The matrix of the SWAP gate.

Return type:

Tensor

Examples

SWAP = swap()
print(f'The SWAP Gate is:\n{SWAP}')
The SWAP Gate is:
tensor([[1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
        [0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j],
        [0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j],
        [0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j]])
quairkit.database.matrix.cp_gate(theta)

Generate the CP gate matrix.

\[\begin{split}CP(\theta) = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & e^{i\theta} \end{bmatrix}\end{split}\]
Parameters:
theta : ndarray | Tensor | Iterable[float] | float

the (batched) parameter, with shape [1].

Returns:

The matrix of the CP gate.

Return type:

ndarray | Tensor

Examples

theta = torch.tensor([torch.pi / 4])
CP = cp(theta)
print(f'The CP Gate is:\n{CP}')
The CP Gate is:
tensor([[1.0000+0.0000j, 0.0000+0.0000j, 0.0000+0.0000j, 0.0000+0.0000j],
        [0.0000+0.0000j, 1.0000+0.0000j, 0.0000+0.0000j, 0.0000+0.0000j],
        [0.0000+0.0000j, 0.0000+0.0000j, 1.0000+0.0000j, 0.0000+0.0000j],
        [0.0000+0.0000j, 0.0000+0.0000j, 0.0000+0.0000j, 0.7071+0.7071j]])
quairkit.database.matrix.crx_gate(theta)

Generate the CR_X gate matrix.

\[\begin{split}CR_X = |0\rangle\langle0| \otimes I + |1\rangle\langle1| \otimes R_X = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & \cos\frac{\theta}{2} & -i\sin\frac{\theta}{2} \\ 0 & 0 & -i\sin\frac{\theta}{2} & \cos\frac{\theta}{2} \end{bmatrix}\end{split}\]
Parameters:
theta : ndarray | Tensor | Iterable[float] | float

the (batched) parameter, with shape [1].

Returns:

The matrix of the CR_X gate.

Return type:

ndarray | Tensor

Examples

theta = torch.tensor([torch.pi / 4])
CR_X = crx(theta)
print(f'The CR_X Gate is:\n{CR_X}')
The CR_X Gate is:
tensor([[1.0000+0.0000j, 0.+0.0000j, 0.+0.0000j, 0.+0.0000j],
        [0.+0.0000j, 1.0000+0.0000j, 0.+0.0000j, 0.+0.0000j],
        [0.+0.0000j, 0.+0.0000j, 0.9239+0.0000j, 0.+-0.3827j],
        [0.+0.0000j, 0.+0.0000j, 0.+-0.3827j, 0.9239+0.0000j]])
quairkit.database.matrix.cry_gate(theta)

Generate the CR_Y gate matrix.

\[\begin{split}CR_Y = |0\rangle\langle0| \otimes I + |1\rangle\langle1| \otimes R_Y = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & \cos\frac{\theta}{2} & -\sin\frac{\theta}{2} \\ 0 & 0 & \sin\frac{\theta}{2} & \cos\frac{\theta}{2} \end{bmatrix}\end{split}\]
Parameters:
theta : ndarray | Tensor | Iterable[float] | float

the (batched) parameter, with shape [1].

Returns:

The matrix of the CR_Y gate.

Return type:

ndarray | Tensor

Examples

theta = torch.tensor([torch.pi / 4])
CR_Y = cry(theta)
print(f'The CR_Y Gate is:\n{CR_Y}')
The CR_Y Gate is:
tensor([[1.0000+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
        [0.+0.j, 1.0000+0.j, 0.+0.j, 0.+0.j],
        [0.+0.j, 0.+0.j, 0.9239+0.j, -0.3827+0.j],
        [0.+0.j, 0.+0.j, 0.3827+0.j, 0.9239+0.j]])
quairkit.database.matrix.crz_gate(theta)

Generate the CR_Z gate matrix.

\[\begin{split}CR_Z = |0\rangle\langle0| \otimes I + |1\rangle\langle1| \otimes R_Z = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & e^{-i\theta/2} & 0 \\ 0 & 0 & 0 & e^{i\theta/2} \end{bmatrix}\end{split}\]
Parameters:
theta : ndarray | Tensor | Iterable[float] | float

the (batched) parameter, with shape [1].

Returns:

The matrix of the CR_Z gate.

Return type:

ndarray | Tensor

Examples

theta = torch.tensor([torch.pi / 4])
CR_Z = crz(theta)
print(f'The CR_Z Gate is:\n{CR_Z}')
The CR_Z Gate is:
tensor([[1.0000+0.0000j, 0.+0.0000j, 0.+0.0000j, 0.+0.0000j],
        [0.+0.0000j, 1.0000+0.0000j, 0.+0.0000j, 0.+0.0000j],
        [0.+0.0000j, 0.+0.0000j, 0.9239-0.3827j, 0.+0.0000j],
        [0.+0.0000j, 0.+0.0000j, 0.+0.0000j, 0.9239+0.3827j]])
quairkit.database.matrix.cu_gate(theta)

Generate the CU gate matrix.

\[\begin{split}CU = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & e^{i\gamma}\cos\frac{\theta}{2} & -e^{i(\lambda+\gamma)}\sin\frac{\theta}{2} \\ 0 & 0 & e^{i(\phi+\gamma)}\sin\frac{\theta}{2} & e^{i(\phi+\lambda+\gamma)}\cos\frac{\theta}{2} \end{bmatrix}\end{split}\]
Parameters:
theta : ndarray | Tensor | Iterable[float]

the (batched) parameters, with shape [4, 1].

Returns:

The matrix of the CU gate.

Return type:

ndarray | Tensor

Examples

theta = torch.tensor([[torch.pi / 4], [torch.pi / 3], [torch.pi / 6], [torch.pi / 6]])
CU = cu(theta)
print(f'The CU Gate is:\n{CU}')
The CU Gate is:
tensor([[1.0000e+00+0.0000j, 0.0000e+00+0.0000j, 0.0000e+00+0.0000j, 0.0000e+00+0.0000j],
        [0.0000e+00+0.0000j, 1.0000e+00+0.0000j, 0.0000e+00+0.0000j, 0.0000e+00+0.0000j],
        [0.0000e+00+0.0000j, 0.0000e+00+0.0000j, 8.0010e-01+0.4619j, -1.9134e-01-0.3314j],
        [0.0000e+00+0.0000j, 0.0000e+00+0.0000j, -1.6728e-08+0.3827j, -4.6194e-01+0.8001j]])
quairkit.database.matrix.rxx_gate(theta)

Generate the RXX gate matrix.

\[\begin{split}R_{XX}(\theta) = \begin{bmatrix} \cos\frac{\theta}{2} & 0 & 0 & -i\sin\frac{\theta}{2} \\ 0 & \cos\frac{\theta}{2} & -i\sin\frac{\theta}{2} & 0 \\ 0 & -i\sin\frac{\theta}{2} & \cos\frac{\theta}{2} & 0 \\ -i\sin\frac{\theta}{2} & 0 & 0 & \cos\frac{\theta}{2} \end{bmatrix}\end{split}\]
Parameters:
theta : ndarray | Tensor | Iterable[float] | float

the (batched) parameter, with shape [1].

Returns:

The matrix of the RXX gate.

Return type:

ndarray | Tensor

Examples

theta = torch.tensor([torch.pi / 4])
R_XX = rxx(theta)
print(f'The R_XX Gate is:\n{R_XX}')
The R_XX Gate is:
tensor([[0.9239+0.0000j, 0.0000+0.0000j, 0.0000+0.0000j, 0.0000-0.3827j],
        [0.0000+0.0000j, 0.9239+0.0000j, 0.0000-0.3827j, 0.0000+0.0000j],
        [0.0000+0.0000j, 0.0000-0.3827j, 0.9239+0.0000j, 0.0000+0.0000j],
        [0.0000-0.3827j, 0.0000+0.0000j, 0.0000+0.0000j, 0.9239+0.0000j]])
quairkit.database.matrix.ryy_gate(theta)

Generate the RYY gate matrix.

\[\begin{split}R_{YY}(\theta) = \begin{bmatrix} \cos\frac{\theta}{2} & 0 & 0 & i\sin\frac{\theta}{2} \\ 0 & \cos\frac{\theta}{2} & -i\sin\frac{\theta}{2} & 0 \\ 0 & -i\sin\frac{\theta}{2} & \cos\frac{\theta}{2} & 0 \\ i\sin\frac{\theta}{2} & 0 & 0 & \cos\frac{\theta}{2} \end{bmatrix}\end{split}\]
Parameters:
theta : ndarray | Tensor | Iterable[float] | float

the (batched) parameter, with shape [1].

Returns:

The matrix of the RYY gate.

Return type:

ndarray | Tensor

Examples

theta = torch.tensor([torch.pi / 4])
R_YY = ryy(theta)
print(f'The R_YY Gate is:\n{R_YY}')
The R_YY Gate is:
tensor([[0.9239+0.0000j, 0.0000+0.0000j, 0.0000+0.0000j, 0.0000+0.3827j],
        [0.0000+0.0000j, 0.9239+0.0000j, 0.0000-0.3827j, 0.0000+0.0000j],
        [0.0000+0.0000j, 0.0000-0.3827j, 0.9239+0.0000j, 0.0000+0.0000j],
        [0.0000+0.3827j, 0.0000+0.0000j, 0.0000+0.0000j, 0.9239+0.0000j]])
quairkit.database.matrix.rzz_gate(theta)

Generate the RZZ gate matrix.

\[\begin{split}R_{ZZ}(\theta) = \begin{bmatrix} e^{-i\theta/2} & 0 & 0 & 0 \\ 0 & e^{i\theta/2} & 0 & 0 \\ 0 & 0 & e^{i\theta/2} & 0 \\ 0 & 0 & 0 & e^{-i\theta/2} \end{bmatrix}\end{split}\]
Parameters:
theta : ndarray | Tensor | Iterable[float] | float

the (batched) parameter, with shape [1].

Returns:

The matrix of the RZZ gate.

Return type:

ndarray | Tensor

Examples

theta = torch.tensor([torch.pi / 4])
R_ZZ = rzz(theta)
print(f'The R_ZZ Gate is:\n{R_ZZ}')
The R_ZZ Gate is:
tensor([[0.9239-0.3827j, 0.0000+0.0000j, 0.0000+0.0000j, 0.0000+0.0000j],
        [0.0000+0.0000j, 0.9239+0.3827j, 0.0000+0.0000j, 0.0000+0.0000j],
        [0.0000+0.0000j, 0.0000+0.0000j, 0.9239+0.3827j, 0.0000+0.0000j],
        [0.0000+0.0000j, 0.0000+0.0000j, 0.0000+0.0000j, 0.9239-0.3827j]])
quairkit.database.matrix.ms_gate()

Generate the MS gate matrix.

\[\begin{split}MS = R_{XX}\left(-\frac{\pi}{2}\right) = \frac{1}{\sqrt{2}} \begin{bmatrix} 1 & 0 & 0 & i \\ 0 & 1 & i & 0 \\ 0 & i & 1 & 0 \\ i & 0 & 0 & 1 \end{bmatrix}\end{split}\]
Returns:

The matrix of the MS gate.

Return type:

Tensor

Examples

MS = ms()
print(f'The MS Gate is:\n{MS}')
The MS Gate is:
tensor([[0.7071+0.0000j, 0.0000+0.0000j, 0.0000+0.0000j, 0.0000+0.7071j],
        [0.0000+0.0000j, 0.7071+0.0000j, 0.0000+0.7071j, 0.0000+0.0000j],
        [0.0000+0.0000j, 0.0000+0.7071j, 0.7071+0.0000j, 0.0000+0.0000j],
        [0.0000+0.7071j, 0.0000+0.0000j, 0.0000+0.0000j, 0.7071+0.0000j]])
quairkit.database.matrix.cswap_gate()

Generate the CSWAP gate matrix.

\[\begin{split}CSWAP = \begin{bmatrix} 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 \end{bmatrix}\end{split}\]
Returns:

The matrix of the CSWAP gate.

Return type:

Tensor

Examples

CSWAP = cswap()
print(f'The CSWAP Gate is:\n{CSWAP}')
The CSWAP Gate is:
tensor([[1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
        [0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
        [0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
        [0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
        [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
        [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j],
        [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j],
        [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j]])
quairkit.database.matrix.toffoli_gate()

Generate the Toffoli gate matrix.

\[\begin{split}Toffoli = \begin{bmatrix} 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 \\ 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 \end{bmatrix}\end{split}\]
Returns:

The matrix of the Toffoli gate.

Return type:

Tensor

Examples

Toffoli = toffoli()
print(f'The Toffoli Gate is:\n{Toffoli}')
The Toffoli Gate is:
tensor([[1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
        [0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
        [0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
        [0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
        [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
        [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j],
        [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j],
        [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j]])
quairkit.database.matrix.universal2_gate(theta)

Generate the universal two-qubit gate matrix.

Parameters:
theta : ndarray | Tensor | Iterable[float]

the (batched) parameter with shape [15].

Returns:

The matrix of the universal two-qubit gate.

Return type:

ndarray | Tensor

Examples

theta = torch.tensor([
    0.5, 1.0, 1.5, 2.0, 2.5,
    3.0, 3.5, 4.0, 4.5, 5.0,
    5.5, 6.0, 6.5, 7.0, 7.5])
Universal2 = universal2(theta)
print(f'The matrix of universal two qubits gate is:\n{Universal2}')
The matrix of universal two qubits gate is:
tensor([[-0.2858-0.0270j,  0.4003+0.3090j, -0.6062+0.0791j,  0.5359+0.0323j],
        [-0.0894-0.1008j, -0.5804+0.0194j,  0.3156+0.1677j,  0.7090-0.1194j],
        [-0.8151-0.2697j,  0.2345-0.1841j,  0.3835-0.1154j, -0.0720+0.0918j],
        [-0.2431+0.3212j, -0.1714+0.5374j,  0.1140+0.5703j, -0.2703+0.3289j]])
quairkit.database.matrix.universal3_gate(theta)

Generate the universal three-qubit gate matrix.

Parameters:
theta : ndarray | Tensor | Iterable[float]

the (batched) parameter with shape [81].

Returns:

The matrix of the universal three-qubit gate.

Return type:

ndarray | Tensor

Examples

theta = torch.tensor([
    0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0,
    1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0,
    2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 3.0,
    3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 4.0,
    4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 4.9, 5.0,
    5.1, 5.2, 5.3, 5.4, 5.5, 5.6, 5.7, 5.8, 5.9, 6.0,
    6.1, 6.2, 6.3, 6.4, 6.5, 6.6, 6.7, 6.8, 6.9, 7.0,
    7.1, 7.2, 7.3, 7.4, 7.5, 7.6, 7.7, 7.8, 7.9, 8.0,
    8.1])
Universal3 = universal3(theta)
print(f'The matrix of universal three qubits gate is:\n{Universal3}')
The matrix of universal three qubits gate is:
tensor([[-0.0675-0.0941j, -0.4602+0.0332j,  0.2635+0.0044j,  0.0825+0.3465j,
         -0.0874-0.3635j, -0.1177-0.4195j, -0.2735+0.3619j, -0.1760-0.1052j],
        [ 0.0486+0.0651j, -0.1123+0.0494j,  0.1903+0.0057j, -0.2080+0.2926j,
         -0.2099+0.0630j, -0.1406+0.5173j, -0.1431-0.3538j, -0.5460-0.1847j],
        [ 0.0827-0.0303j,  0.1155+0.1111j,  0.5391-0.0701j, -0.4229-0.2655j,
         -0.1546+0.1943j, -0.0455+0.1744j, -0.3242+0.3539j,  0.3118-0.0041j],
        [-0.1222+0.3984j,  0.1647-0.1817j,  0.3294-0.1486j, -0.0293-0.1503j,
          0.0100-0.6481j,  0.2424+0.1575j,  0.2485+0.0232j, -0.1053+0.1873j],
        [-0.4309-0.0791j, -0.2071-0.0482j, -0.4331+0.0866j, -0.5454-0.1778j,
         -0.1401-0.0230j,  0.0170+0.0299j,  0.0078+0.2231j, -0.2324+0.3369j],
        [ 0.0330+0.3056j,  0.2612+0.6464j, -0.2138-0.1748j, -0.2322-0.0598j,
          0.1387-0.1573j,  0.0914-0.2963j, -0.2712-0.1351j, -0.1272-0.1940j],
        [ 0.0449-0.3844j,  0.1135+0.2846j, -0.0251+0.3854j,  0.0442-0.0149j,
         -0.3671-0.1774j,  0.5158+0.1148j,  0.2151+0.1433j, -0.0188-0.3040j],
        [-0.4124-0.4385j,  0.2306+0.0894j,  0.0104-0.2180j, -0.0180+0.2869j,
         -0.1030-0.2991j, -0.1473+0.0931j, -0.1686-0.3451j,  0.3825+0.1480j]])
quairkit.database.matrix.Uf_gate(f, n)

Construct the unitary matrix used in Simon’s algorithm.

Parameters:
f : Callable[[int], int]

a 2-to-1 or 1-to-1 function \(f: \{0, 1\}^n \to \{0, 1\}^n\).

n : int

length of the bit string.

Returns:

A 2n-qubit unitary matrix satisfying

\[U|x, y\rangle = |x, y \oplus f(x)\rangle\]

Return type:

Tensor

Examples

def f(x: int) -> int:
    return x

unitary_matrix = Uf(f, 1)
print(f'Unitary matrix is:\n{unitary_matrix}')
Unitary matrix is:
tensor([[1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
        [0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j],
        [0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j],
        [0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j]])
quairkit.database.matrix.Of_gate(f, n)

Construct the oracle unitary matrix for an unstructured search problem.

Parameters:
f : Callable[[int], int]

a function \(f: \{0, 1\}^n \to \{0, 1\}\).

n : int

length of the bit string.

Returns:

An n-qubit unitary matrix satisfying

\[U|x\rangle = (-1)^{f(x)}|x\rangle\]

Return type:

Tensor

Examples

def f(x: int) -> int:
    return x

unitary_matrix = Of(f, 1)
print(f'Unitary matrix is:\n{unitary_matrix}')
Unitary matrix is:
tensor([[ 1.+0.j,  0.+0.j],
        [ 0.+0.j, -1.+0.j]])