QPanda3  0.1.0
Supported by OriginQ
Loading...
Searching...
No Matches
Quantum Gate

Prev Tutorial: QPanda3 Tutorials
Next Tutorial: Quantum Circuit and Quantum Program

Quantum Logic Gates

In classical computing, the most basic unit is the bit, and the fundamental control mechanism is the logic gate. We can achieve our control of circuits through combinations of logic gates. Similarly, the way we manipulate qubits is through quantum logic gates. By using quantum logic gates, we consciously induce the evolution of quantum states. Thus, quantum logic gates form the foundation of quantum algorithms.

Quantum logic gates are represented by unitary matrices. The most common quantum gates operate in the space of one or two qubits, just as common classical logic gates operate on one or two bits.

Common Quantum Logic Gates in Matrix Form

Gate Name Matrix
I I

\[\begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix}\]

H Hadamard

\[\begin{bmatrix} \frac{1}{\sqrt{2}} & \frac{1}{\sqrt{2}} \\ \frac{1}{\sqrt{2}} & -\frac{1}{\sqrt{2}} \end{bmatrix}\]

T T

\[\begin{bmatrix} 1 & 0 \\ 0 & \exp\left(\frac{i\pi}{4}\right) \end{bmatrix}\]

S S

\[\begin{bmatrix} 1 & 0 \\ 0 & i \end{bmatrix}\]

X Pauli-X

\[\begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix}\]

Y Pauli-Y

\[\begin{bmatrix} 0 & -i \\ i & 0 \end{bmatrix}\]

Z Pauli-Z

\[\begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix}\]

X1 X1

\[\begin{bmatrix} \frac{1}{\sqrt{2}} & -\frac{i}{\sqrt{2}} \\ -\frac{i}{\sqrt{2}} & \frac{1}{\sqrt{2}} \end{bmatrix}\]

Y1 Y1

\[\begin{bmatrix} \frac{1}{\sqrt{2}} & -\frac{1}{\sqrt{2}} \\ \frac{1}{\sqrt{2}} & \frac{1}{\sqrt{2}} \end{bmatrix}\]

Z1 Z1

\[\begin{bmatrix} \exp\left(-\frac{i\pi}{4}\right) & 0 \\ 0 & \exp\left(\frac{i\pi}{4}\right) \end{bmatrix}\]

RX RX

\[\begin{bmatrix} \cos\left(\frac{\theta}{2}\right) & -i\sin\left(\frac{\theta}{2}\right) \\ -i\sin\left(\frac{\theta}{2}\right) & \cos\left(\frac{\theta}{2}\right) \end{bmatrix}\]

RY RY

\[\begin{bmatrix} \cos\left(\frac{\theta}{2}\right) & -\sin\left(\frac{\theta}{2}\right) \\ \sin\left(\frac{\theta}{2}\right) & \cos\left(\frac{\theta}{2}\right) \end{bmatrix}\]

RZ RZ

\[\begin{bmatrix} \exp\left(-\frac{i\theta}{2}\right) & 0 \\ 0 & \exp\left(\frac{i\theta}{2}\right) \end{bmatrix}\]

U1 U1

\[\begin{bmatrix} 1 & 0 \\ 0 & \exp(i\theta) \end{bmatrix}\]

U2 U2

\[\begin{bmatrix} \frac{1}{\sqrt{2}} & -\frac{\exp(i\lambda)}{\sqrt{2}} \\ \frac{\exp(i\phi)}{\sqrt{2}} & \frac{\exp(i\lambda + i\phi)}{\sqrt{2}} \end{bmatrix}\]

U3 U3

\[\begin{bmatrix} \cos\left(\frac{\theta}{2}\right) & -\exp(i\lambda)\sin\left(\frac{\theta}{2}\right) \\ \exp(i\phi)\sin\left(\frac{\theta}{2}\right) & \exp(i\lambda + i\phi)\cos\left(\frac{\theta}{2}\right) \end{bmatrix}\]

U4 U4

\[\begin{bmatrix} u_0 & u_1 \\ u_2 & u_3 \end{bmatrix}\]

Multi-qubit Quantum Gates

Gate Name Matrix
CNOT CNOT

\[\begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0 \end{bmatrix}\]

CR CR

\[\begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & \exp(i\theta) \end{bmatrix}\]

iSWAP iSWAP

\[\begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & \cos(\theta) & i\sin(\theta) & 0 \\ 0 & i\sin(\theta) & \cos(\theta) & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}\]

SWAP SWAP

\[\begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}\]

CZ CZ

\[\begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & -1 \end{bmatrix}\]

CU CU

\[\begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & u_0 & u_1 \\ 0 & 0 & u_2 & u_3 \end{bmatrix}\]

RXX RXX

\[\begin{bmatrix} \cos(\theta/2) & 0 & 0 & -i\sin(\theta/2) \\ 0 & \cos(\theta/2) & -i\sin(\theta/2) & 0 \\ 0 & -i\sin(\theta/2) & \cos(\theta/2) & 0 \\ -i\sin(\theta/2) & 0 & 0 & \cos(\theta/2) \end{bmatrix}\]

RYY RYY

\[\begin{bmatrix} \cos(\theta/2) & 0 & 0 & i\sin(\theta/2) \\ 0 & \cos(\theta/2) & -i\sin(\theta/2) & 0 \\ 0 & -i\sin(\theta/2) & \cos(\theta/2) & 0 \\ i\sin(\theta/2) & 0 & 0 & \cos(\theta/2) \end{bmatrix}\]

RZZ RZZ

\[\begin{bmatrix} \exp(-i\theta/2) & 0 & 0 & 0 \\ 0 & \exp(i\theta/2) & 0 & 0 \\ 0 & 0 & \exp(i\theta/2) & 0 \\ 0 & 0 & 0 & \exp(-i\theta/2) \end{bmatrix}\]

RZX RZX

\[\begin{bmatrix} \cos(\theta/2) & 0 & -i\sin(\theta/2) & 0 \\ 0 & \cos(\theta/2) & 0 & i\sin(\theta/2) \\ -i\sin(\theta/2) & 0 & \cos(\theta/2) & 0 \\ 0 & i\sin(\theta/2) & 0 & \cos(\theta/2) \end{bmatrix}\]

Toffoli 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 & 1 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 \\ \end{bmatrix}\]

API Introduction

QPanda3 encapsulates all quantum logic gates as APIs for user access, returning values of type QGate. For example, if you want to use the Hadamard gate, you can obtain it as follows:

As you can see, the H function only takes one qubit. Similarly, if you want to use the RX gate, you can obtain it like this:

As shown, the RX gate takes two parameters: the first is the target qubit, and the second is the rotation angle. You can also use the same method for the RY and RZ gates.

The usage of two-qubit quantum logic gates is similar to that of single-qubit gates, but the input parameters differ. For example, using the CNOT gate:

The CNOT gate takes two parameters: the first is the control qubit, and the second is the target qubit.

Interface Overview

Here we will introduce all the interfaces related to the QGate class. Firstly, we need to know where to import the QGate class from

Querying Qubits

  • Get Number of Qubits

    Using the qubits_num function to get this gate num of qubits . Example:

  • Get Qubits

    Using the qubits function to get this gate all qubits. Example:

  • Get Control Qubits

    Using the control_qubits function to get this gate control_qubits. Example:

  • Get Target Qubits

    Using the target_qubits function to get this gate target_qubits. Example:

Gate Properties

  • Dagger Operation

    As mentioned at the beginning of this chapter, all quantum logic gates are unitary matrices, and you can also perform conjugate transpose operations on quantum logic gates. The QGate type has two member functions that can perform conjugate transpose operations: dagger.

    The dagger function creates a copy of the current quantum logic gate and updates the copied gate's dagger flag. For example:

  • Raise Gate to a Power

    Using the power function to get this gate raised to a power. Example:

  • Control Operations

    In addition to the conjugate transpose operation, you can also add control qubits to the quantum logic gates. After adding control qubits, whether the current quantum logic gate executes depends on the quantum state of the control qubits. If the control qubit's state is \(|1\rangle\), the current quantum logic gate can be executed; if the state is \(|0\rangle\), the gate will not execute. The QGate type has two member functions to help you add control qubits: control and add_control.

    The add_control function creates a copy of the current quantum and adds control qubits to the current quantum logic gate. For example:

    The control function creates a copy of the current quantum logic gate and set control qubits to the copied gate. For example:

  • Clear Control Qubits

    The clear_control function clear control qubits of gate. For example:

Matrix Operations

  • Get Matrix Representation

    Using the matrix function to obtain a matrix.

    Note
    The meaning of the expanded parameter is whether the matrix is obtained based on the order of qubits,for example, the matrix of CNOT (1,0) is equivalent to CNOT (0,1) when the parameter is False.

    Example:

Gate Type and Parameters

  • Get Gate Type

    Using the gate_type function to get gate type. Example:

  • Get and Set Parameters

    Using the parameters function to get gate parameters. Using the set_parameters function to set gate parameters. Example:

  • Get Gate Name

    Using the name function to get gate name.

    Example: