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

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

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

Return type:

ndarray | Tensor

\[G = A (2 |0^n \rangle\langle 0^n| - I^n) A^\dagger \cdot (I - 2|1 \rangle\langle 1|) \otimes I^{n-1}\]
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 gate in below matrix form, here \(\omega_N = \text{exp}(\frac{2 \pi i}{N})\)

Return type:

Tensor

\[\begin{split}\begin{align} QFT = \frac{1}{\sqrt{N}} \begin{bmatrix} 1 & 1 & .. & 1 \\ 1 & \omega_N & .. & \omega_N^{N-1} \\ .. & .. & .. & .. \\ 1 & \omega_N^{N-1} & .. & \omega_N^{(N-1)^2} \end{bmatrix} \end{align}\end{split}\]
num_qubits = 1
qft_gate = qft_matrix(num_qubits)
print(f'The QTF gate is:\n{qft_gate}')
The QTF 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

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

the matrix of H gate.

Return type:

Tensor

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

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

the matrix of S gate.

Return type:

Tensor

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

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

the matrix of Sdg gate.

Return type:

Tensor

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

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

the matrix of T gate.

Return type:

Tensor

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

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

the matrix of Tdg gate.

Return type:

Tensor

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 matrix (when dim = 2)

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

the dimension of the identity matrix. Defaults to the qubit case.

Returns:

the matrix of X gate.

Return type:

Tensor

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 matrix

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

the matrix of X gate.

Return type:

Tensor

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 matrix

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

the matrix of Y gate.

Return type:

Tensor

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 matrix

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

the matrix of Z gate.

Return type:

Tensor

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 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 parameter of this matrix. The shape of param is [1]

Returns:

the matrix of P gate.

Return type:

ndarray | Tensor

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 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 parameter of this matrix. The shape of param is [1]

Returns:

the matrix of R_X gate.

Return type:

ndarray | Tensor

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 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 parameter of this matrix. The shape of param is [1]

Returns:

the matrix of R_Y gate.

Return type:

ndarray | Tensor

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 matrix

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

the parameter of this matrix. The shape of param is [1]

Returns:

the matrix of R_Z gate.

Return type:

ndarray | Tensor

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 matrix

\[\begin{split}\begin{align} U_3(\theta, \phi, \lambda) = \begin{bmatrix} \cos\frac\theta2&-e^{i\lambda}\sin\frac\theta2\\ e^{i\phi}\sin\frac\theta2&e^{i(\phi+\lambda)}\cos\frac\theta2 \end{bmatrix} \end{align}\end{split}\]
Parameters:
theta : ndarray | Tensor | Iterable[float]

the parameter of this matrix. The shape of param is [3, 1]

Returns:

the matrix of U_3 gate.

Return type:

ndarray | Tensor

theta = torch.tensor([[torch.pi / 4], [torch.pi / 3], [torch.pi / 6]])
u3_matrix = u3(theta)
print(f'The U_3 Gate is:\n{u3_matrix}')
The U_3 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 matrix

\[\begin{split}\begin{align} \mathit{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{align}\end{split}\]
Returns:

the matrix of CNOT gate.

Return type:

Tensor

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 matrix

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

the matrix of CY gate.

Return type:

Tensor

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 matrix

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

the matrix of CZ gate.

Return type:

Tensor

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 matrix

\[\begin{split}\begin{align} \mathit{SWAP} = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} \end{align}\end{split}\]
Parameters:
dtype

the dtype of this matrix. Defaults to None.

Returns:

the matrix of SWAP gate.

Return type:

Tensor

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 matrix

\[\begin{split}\begin{align} \mathit{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{align}\end{split}\]
Parameters:
theta : ndarray | Tensor | Iterable[float] | float

the parameter of this matrix. The shape of param is [1]

Returns:

the matrix of CP gate.

Return type:

ndarray | Tensor

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 matrix

\[\begin{split}\begin{align} \mathit{CR_X} &=|0\rangle \langle 0|\otimes I + |1 \rangle \langle 1|\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{align}\end{split}\]
Parameters:
theta : ndarray | Tensor | Iterable[float] | float

the parameter of this matrix. The shape of param is [1]

Returns:

the matrix of CR_X gate.

Return type:

ndarray | Tensor

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.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.9239+0.0000j, 0.0000-0.3827j],
        [0.0000+0.0000j, 0.0000+0.0000j, 0.0000-0.3827j, 0.9239+0.0000j]])
quairkit.database.matrix.cry(theta)

Generate the matrix

\[\begin{split}\begin{align} \mathit{CR_Y} &=|0\rangle \langle 0|\otimes I + |1 \rangle \langle 1|\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{align}\end{split}\]
Parameters:
theta : ndarray | Tensor | Iterable[float] | float

the parameter of this matrix. The shape of param is [1]

Returns:

the matrix of CR_Y gate.

Return type:

ndarray | Tensor

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.0000+0.j,  0.0000+0.j,  0.0000+0.j],
        [ 0.0000+0.j,  1.0000+0.j,  0.0000+0.j,  0.0000+0.j],
        [ 0.0000+0.j,  0.0000+0.j,  0.9239+0.j, -0.3827+0.j],
        [ 0.0000+0.j,  0.0000+0.j,  0.3827+0.j,  0.9239+0.j]])
quairkit.database.matrix.crz(theta)

Generate the matrix

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

the parameter of this matrix. The shape of param is [1]

Returns:

the matrix of CR_Z gate.

Return type:

ndarray | Tensor

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.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.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.cu(theta)

Generate the matrix

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

the parameter of this matrix. The shape of param is [4,1]

Returns:

the matrix of CU gate.

Return type:

ndarray | Tensor

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 matrix

\[\begin{split}\begin{align} \mathit{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{align}\end{split}\]
Parameters:
theta : ndarray | Tensor | Iterable[float] | float

the parameter of this matrix. The shape of param is [1]

Returns:

the matrix of RXX gate.

Return type:

ndarray | Tensor

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 matrix

\[\begin{split}\begin{align} \mathit{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{align}\end{split}\]
Parameters:
theta : ndarray | Tensor | Iterable[float] | float

the parameter of this matrix. The shape of param is [1]

Returns:

the matrix of RYY gate.

Return type:

ndarray | Tensor

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 matrix

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

the parameter of this matrix. The shape of param is [1]

Returns:

the matrix of RZZ gate.

Return type:

ndarray | Tensor

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 matrix

\[\begin{split}\begin{align} \mathit{MS} = \mathit{R_{XX}}(-\frac{\pi}{2}) = \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{align}\end{split}\]
Returns:

the matrix of MS gate.

Return type:

Tensor

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 matrix

\[\begin{split}\begin{align} \mathit{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 & 0 & 1 & 0 \\ 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 \end{bmatrix} \end{align}\end{split}\]
Parameters:
dtype

the dtype of this matrix. Defaults to None.

Returns:

the matrix of CSWAP gate.

Return type:

Tensor

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, 0.+0.j, 1.+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, 0.+0.j, 1.+0.j]])
quairkit.database.matrix.toffoli()

Generate the matrix

\[\begin{split}\begin{align} \mathit{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{align}\end{split}\]
Parameters:
dtype

the dtype of this matrix. Defaults to None.

Returns:

the matrix of Toffoli gate.

Return type:

Tensor

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, 0.+0.j, 1.+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 matrix

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

the parameter of this matrix. The shape of param is [15]

Returns:

the matrix of universal two qubits gate.

Return type:

ndarray | Tensor

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 matrix

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

the parameter of this matrix. The shape of param is [81]

Returns:

the matrix of universal three qubits gate.

Return type:

ndarray | Tensor

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)

Generalized GellMann matrix basis were used to construct the universal gate for qudits

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

the parameter of this matrix. The shape of param is [dimension**2 - 1]

dimension : int

the dimension of the qudit

Returns:

the matrix of d-dimensional unitary gate

Return type:

ndarray | Tensor

References

[wolfram mathworld](https://mathworld.wolfram.com/GeneralizedGell-MannMatrix.html)

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 maps \(|x\rangle|y\rangle\) to \(|x\rangle|y\oplus f(x)\rangle\) based on a boolean function \(f\).

Parameters:
f : Callable[[Tensor], Tensor]

a boolean function \(f\) that maps \(\{ 0,1 \}^{n}\) to \(\{ 0,1 \}\);

n : int

the length of input \(\{ 0,1 \}^{n}\);

Returns:

Unitary matrix in form

Return type:

Tensor

\[U: U|x\rangle|y\rangle = |x\rangle|y\oplus f(x)\rangle\]
n=2

def xor_function(x: torch.Tensor) -> torch.Tensor:
    real_x = x.real
    return torch.tensor(int(real_x[0].item()) ^ int(real_x[1].item()))

f = xor_function
unitary_matrix = Uf(f, n)

print(f'Unitary matrix in form is:\n{unitary_matrix}')
Unitary matrix in form 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, 0.+0.j, 1.+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, 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, 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, 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]])

Note

for a boolean function ‘f’, ‘x = torch.zeros(n)’ is a legitimate input, but ‘x = torch.zeros((n,1))’ is an illegitimate input

quairkit.database.matrix.Of(f, n)

Construct the unitary matrix maps \(|x\rangle\) to \((-1)^{f(x)}|x\rangle\) based on a boolean function \(f\).

Parameters:
f : Callable[[Tensor], Tensor]

a boolean function \(f\) that maps \(\{ 0,1 \}^{n}\) to \(\{ 0,1 \}\);

n : int

the length of input \(\{ 0,1 \}^{n}\);

Returns:

Unitary matrix in form

Return type:

Tensor

\[U: U|x\rangle = (-1)^{f(x)}|x\rangle\]
n=2
def xor_function(x: torch.Tensor) -> torch.Tensor:
    real_x = x.real
    return torch.tensor(int(real_x[0].item()) ^ int(real_x[1].item()))

f = xor_function
unitary_matrix = Of(f, n)
print(f'Unitary matrix in form is:\n{unitary_matrix}')
Unitary matrix in form 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]])

Note

for a boolean function ‘f’, ‘x = torch.zeros(n)’ is a legitimate input, but ‘x = torch.zeros((n,1))’ is an illegitimate input

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

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

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

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

the matrix of H gate.

Return type:

Tensor

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

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

the matrix of S gate.

Return type:

Tensor

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

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

the matrix of Sdg gate.

Return type:

Tensor

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

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

the matrix of T gate.

Return type:

Tensor

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

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

the matrix of Tdg gate.

Return type:

Tensor

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 matrix (when dim = 2)

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

the dimension of the identity matrix. Defaults to the qubit case.

Returns:

the matrix of X gate.

Return type:

Tensor

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 matrix

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

the matrix of X gate.

Return type:

Tensor

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 matrix

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

the matrix of Y gate.

Return type:

Tensor

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 matrix

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

the matrix of Z gate.

Return type:

Tensor

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 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 parameter of this matrix. The shape of param is [1]

Returns:

the matrix of P gate.

Return type:

ndarray | Tensor

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 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 parameter of this matrix. The shape of param is [1]

Returns:

the matrix of R_X gate.

Return type:

ndarray | Tensor

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 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 parameter of this matrix. The shape of param is [1]

Returns:

the matrix of R_Y gate.

Return type:

ndarray | Tensor

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 matrix

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

the parameter of this matrix. The shape of param is [1]

Returns:

the matrix of R_Z gate.

Return type:

ndarray | Tensor

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 matrix

\[\begin{split}\begin{align} U_3(\theta, \phi, \lambda) = \begin{bmatrix} \cos\frac\theta2&-e^{i\lambda}\sin\frac\theta2\\ e^{i\phi}\sin\frac\theta2&e^{i(\phi+\lambda)}\cos\frac\theta2 \end{bmatrix} \end{align}\end{split}\]
Parameters:
theta : ndarray | Tensor | Iterable[float]

the parameter of this matrix. The shape of param is [3, 1]

Returns:

the matrix of U_3 gate.

Return type:

ndarray | Tensor

theta = torch.tensor([[torch.pi / 4], [torch.pi / 3], [torch.pi / 6]])
u3_matrix = u3(theta)
print(f'The U_3 Gate is:\n{u3_matrix}')
The U_3 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 matrix

\[\begin{split}\begin{align} \mathit{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{align}\end{split}\]
Returns:

the matrix of CNOT gate.

Return type:

Tensor

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 matrix

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

the matrix of CY gate.

Return type:

Tensor

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 matrix

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

the matrix of CZ gate.

Return type:

Tensor

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 matrix

\[\begin{split}\begin{align} \mathit{SWAP} = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} \end{align}\end{split}\]
Parameters:
dtype

the dtype of this matrix. Defaults to None.

Returns:

the matrix of SWAP gate.

Return type:

Tensor

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 matrix

\[\begin{split}\begin{align} \mathit{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{align}\end{split}\]
Parameters:
theta : ndarray | Tensor | Iterable[float] | float

the parameter of this matrix. The shape of param is [1]

Returns:

the matrix of CP gate.

Return type:

ndarray | Tensor

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 matrix

\[\begin{split}\begin{align} \mathit{CR_X} &=|0\rangle \langle 0|\otimes I + |1 \rangle \langle 1|\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{align}\end{split}\]
Parameters:
theta : ndarray | Tensor | Iterable[float] | float

the parameter of this matrix. The shape of param is [1]

Returns:

the matrix of CR_X gate.

Return type:

ndarray | Tensor

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.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.9239+0.0000j, 0.0000-0.3827j],
        [0.0000+0.0000j, 0.0000+0.0000j, 0.0000-0.3827j, 0.9239+0.0000j]])
quairkit.database.matrix.cry_gate(theta)

Generate the matrix

\[\begin{split}\begin{align} \mathit{CR_Y} &=|0\rangle \langle 0|\otimes I + |1 \rangle \langle 1|\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{align}\end{split}\]
Parameters:
theta : ndarray | Tensor | Iterable[float] | float

the parameter of this matrix. The shape of param is [1]

Returns:

the matrix of CR_Y gate.

Return type:

ndarray | Tensor

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.0000+0.j,  0.0000+0.j,  0.0000+0.j],
        [ 0.0000+0.j,  1.0000+0.j,  0.0000+0.j,  0.0000+0.j],
        [ 0.0000+0.j,  0.0000+0.j,  0.9239+0.j, -0.3827+0.j],
        [ 0.0000+0.j,  0.0000+0.j,  0.3827+0.j,  0.9239+0.j]])
quairkit.database.matrix.crz_gate(theta)

Generate the matrix

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

the parameter of this matrix. The shape of param is [1]

Returns:

the matrix of CR_Z gate.

Return type:

ndarray | Tensor

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.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.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.cu_gate(theta)

Generate the matrix

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

the parameter of this matrix. The shape of param is [4,1]

Returns:

the matrix of CU gate.

Return type:

ndarray | Tensor

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 matrix

\[\begin{split}\begin{align} \mathit{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{align}\end{split}\]
Parameters:
theta : ndarray | Tensor | Iterable[float] | float

the parameter of this matrix. The shape of param is [1]

Returns:

the matrix of RXX gate.

Return type:

ndarray | Tensor

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 matrix

\[\begin{split}\begin{align} \mathit{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{align}\end{split}\]
Parameters:
theta : ndarray | Tensor | Iterable[float] | float

the parameter of this matrix. The shape of param is [1]

Returns:

the matrix of RYY gate.

Return type:

ndarray | Tensor

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 matrix

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

the parameter of this matrix. The shape of param is [1]

Returns:

the matrix of RZZ gate.

Return type:

ndarray | Tensor

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 matrix

\[\begin{split}\begin{align} \mathit{MS} = \mathit{R_{XX}}(-\frac{\pi}{2}) = \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{align}\end{split}\]
Returns:

the matrix of MS gate.

Return type:

Tensor

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 matrix

\[\begin{split}\begin{align} \mathit{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 & 0 & 1 & 0 \\ 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 \end{bmatrix} \end{align}\end{split}\]
Parameters:
dtype

the dtype of this matrix. Defaults to None.

Returns:

the matrix of CSWAP gate.

Return type:

Tensor

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, 0.+0.j, 1.+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, 0.+0.j, 1.+0.j]])
quairkit.database.matrix.toffoli_gate()

Generate the matrix

\[\begin{split}\begin{align} \mathit{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{align}\end{split}\]
Parameters:
dtype

the dtype of this matrix. Defaults to None.

Returns:

the matrix of Toffoli gate.

Return type:

Tensor

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, 0.+0.j, 1.+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 matrix

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

the parameter of this matrix. The shape of param is [15]

Returns:

the matrix of universal two qubits gate.

Return type:

ndarray | Tensor

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 matrix

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

the parameter of this matrix. The shape of param is [81]

Returns:

the matrix of universal three qubits gate.

Return type:

ndarray | Tensor

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 maps \(|x\rangle|y\rangle\) to \(|x\rangle|y\oplus f(x)\rangle\) based on a boolean function \(f\).

Parameters:
f : Callable[[Tensor], Tensor]

a boolean function \(f\) that maps \(\{ 0,1 \}^{n}\) to \(\{ 0,1 \}\);

n : int

the length of input \(\{ 0,1 \}^{n}\);

Returns:

Unitary matrix in form

Return type:

Tensor

\[U: U|x\rangle|y\rangle = |x\rangle|y\oplus f(x)\rangle\]
n=2

def xor_function(x: torch.Tensor) -> torch.Tensor:
    real_x = x.real
    return torch.tensor(int(real_x[0].item()) ^ int(real_x[1].item()))

f = xor_function
unitary_matrix = Uf(f, n)

print(f'Unitary matrix in form is:\n{unitary_matrix}')
Unitary matrix in form 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, 0.+0.j, 1.+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, 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, 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, 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]])

Note

for a boolean function ‘f’, ‘x = torch.zeros(n)’ is a legitimate input, but ‘x = torch.zeros((n,1))’ is an illegitimate input

quairkit.database.matrix.Of_gate(f, n)

Construct the unitary matrix maps \(|x\rangle\) to \((-1)^{f(x)}|x\rangle\) based on a boolean function \(f\).

Parameters:
f : Callable[[Tensor], Tensor]

a boolean function \(f\) that maps \(\{ 0,1 \}^{n}\) to \(\{ 0,1 \}\);

n : int

the length of input \(\{ 0,1 \}^{n}\);

Returns:

Unitary matrix in form

Return type:

Tensor

\[U: U|x\rangle = (-1)^{f(x)}|x\rangle\]
n=2
def xor_function(x: torch.Tensor) -> torch.Tensor:
    real_x = x.real
    return torch.tensor(int(real_x[0].item()) ^ int(real_x[1].item()))

f = xor_function
unitary_matrix = Of(f, n)
print(f'Unitary matrix in form is:\n{unitary_matrix}')
Unitary matrix in form 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]])

Note

for a boolean function ‘f’, ‘x = torch.zeros(n)’ is a legitimate input, but ‘x = torch.zeros((n,1))’ is an illegitimate input