From ddaed548e55d53ba8e801ee240f1356bd0f0e6ea Mon Sep 17 00:00:00 2001 From: chen-pengfei00 <1012673739@qq.com> Date: Tue, 1 Jun 2021 19:27:57 +0800 Subject: [PATCH] update tutorials/training/source_en/advanced_use/parameterized_quantum_circuit.md. --- .../parameterized_quantum_circuit.md | 177 +++++++++++++++++- 1 file changed, 175 insertions(+), 2 deletions(-) diff --git a/tutorials/training/source_en/advanced_use/parameterized_quantum_circuit.md b/tutorials/training/source_en/advanced_use/parameterized_quantum_circuit.md index efa3e922dc..7ffe78acd2 100644 --- a/tutorials/training/source_en/advanced_use/parameterized_quantum_circuit.md +++ b/tutorials/training/source_en/advanced_use/parameterized_quantum_circuit.md @@ -1,5 +1,178 @@ # Parameterized Quantum Circuit + Parameterized quantum circuit,call as PQC,is an approach for quantum machine learning.Quantum classical hybrid machine learning architecture MindQuantum can process quantum circuits with parameters, and use the reversibility of quantum neural network to automatically differentiate the circuits and obtain the derivatives of the observed values with respect to each parameter. +The general process of constructing a parameterized quantum circuit and using the parameterized simulator operator to carry out circuit evolution is as follows : +1. Initialize the quantum circuit. +2. Add parameterized quantum gates or non-parametric quantum gates to the quantum circuit according to requirements. +3. Use PQC simulator operator to conduct state evolution or gradient solution -No English version right now, welcome to contribute. +Environment to prepare + Import the modules that this tutorial depends on. - +============= +import numpy as np +import mindquantum as mq +from mindquantum.gate import H, X, Y, RY, RX +============= + +Quantum Gate +Quantum gate is the basic logic unit that operates on quantum bits. For classical circuits; Any logic circuit can be made up of some basic logic gates. Similarly, any quantum circuit can be made up of some basic quantum gates, such as gates acting on a single bit and controlled non gates. The commonly used quantum gates include X gate, Y gate, Z gate, Hadamard7 gate, CNOT gate and some revolving gates. For example, gate Y has the following form + print('Gate name: ', Y) + print('Gate matrix: \n', Y.matrix()) +Gate name: Y +Gate matrix: + [[ 0.+0.j -0.-1.j] + [ 0.+1.j 0.+0.j]] +The Z gate above is a non-parametric gate, while some revolving gates (such as Ry gate) are parametric gates. By giving different rotation angles of 6, the rotating gate will have different influences on the quantum bit. For example, the Ry gate matrix is expressed as: + +RY(θ)=e−iθY/2=(cos(θ/2)sin(θ/2)−sin(θ/2)cos(θ/2)) + +Where'i' is the imaginary basic unit. The parametric quantum gate is an important component of the quantum neural network. Next, we print the matrix form of RY gate with rotation Angle of 0.5. +ry = RY('a') +ry.matrix({'a': 0.5}) +array([[ 0.96891242, -0.24740396], + [ 0.24740396, 0.96891242]]) + +Quantum Circuit +Quantum circuit is a structure used to organize various quantum logic gates effectively. We can initialize the quantum circuit through the IS of the quantum gate, or expand the quantum circuit by adding (+) a quantum gate or circuit, and multiplying (*) by an integer. Here, we will construct the following quantum circuit and print the relevant information of the quantum circuit. In the figure below, A0.91 and A2 represent three quantum bits respectively. The quantum circuit is composed of three quantum gates, namely, Hadamardi gate acting on Q bit, CNOT gate acting on Q1 bit and controlled by GE bit, and Ry revolving gate acting on A2 bit. + +HIQSimulator is compatible with the quantum circuit construction format, using the circuitEngine CircuitEngine to build the quantum circuit. +We apply the quantum gate to the corresponding qubit through the operator 1". +eng = mq.engine.CircuitEngine() +qubits = eng.allocate_qureg(3) +H | qubits[0] +X | (qubits[0], qubits[1]) +RY('p1') | qubits[2] +encoder = eng.circuit +print(encoder) +encoder.summary() +H(0) +X(1 <-: 0) +RY(p1|2) +========Circuit Summary======== +|Total number of gates : 3. | +|Parameter gates : 1. | +|with 1 parameters are : p1. | +|Number qubit of circuit: 3 | +=============================== + +Here x1 <-: 0) represents the bit-controlled X gate acting on 1 bit, also known as CNOT gate. Ry (P12) represents the revolving door acting on 2 bits around the Y axis, and P1 represents the rotation Angle. According to the printed Summary information, we can find that the quantum circuit is composed of three quantum gates, one of which is a parameterized quantum gate, and the whole quantum circuit reaches three quantum bits. +2. Use decorators to build quantum circuits +Building quantum circuits through decorators eliminates some of the repetitive engine declaration steps. +from mindquantum.engine import circuit_generator + +@circuit_generator(3) +def encoder(qubits): + H | qubits[0] + X | (qubits[0], qubits[1]) + RY('p1') | qubits[2] + +print(encoder) +encoder.summary() +H(0) +X(1 <-: 0) +RY(p1|2) +========Circuit Summary======== +|Total number of gates : 3. | +|Parameter gates : 1. | +|with 1 parameters are : p1. | +|Number qubit of circuit: 3 | +=============================== + +We can also pass more parameters to the decorator to use when the line is generated. For example, you can pass in a string and use this string to add a prefix to each parameter when building a quantum circuit. In this way, you can generate quantum wires with the same structure but different parameter names! + +@circuit_generator(3, prefix='encoder') +def encoder(qubits, prefix): + H | qubits[0] + X | (qubits[0], qubits[1]) + RY(prefix + '_1') | qubits[2] + +print(encoder) +encoder.summary() +H(0) +X(1 <-: 0) +RY(encoder_1|2) +===========Circuit Summary=========== +|Total number of gates : 3. | +|Parameter gates : 1. | +|with 1 parameters are : encoder_1.| +|Number qubit of circuit: 3 | +===================================== + +More convenient way to generate lines +The quantum circuit can be built rapidly by adding quantum gates acting on different bits to the quantum circuit. +from mindquantum import Circuit + +encoder = Circuit() +encoder += H.on(0) +encoder += X.on(1,0) +encoder += RY('p1').on(2) +print(encoder) +encoder.summary() +H(0) +X(1 <-: 0) +RY(p1|2) +========Circuit Summary======== +|Total number of gates : 3. | +|Parameter gates : 1. | +|with 1 parameters are : p1. | +|Number qubit of circuit: 3 | +=============================== + +The Mindspore operator is used to simulate the quantum circuit +A common quantum neural network usually consists of the following three parts: +· One (or more) coded line for encoding classical data into quantum data. +. One (or more) lines to train (often called Ansatz) +· One (or more) physical quantity to be measured +The coding part of the quantum neural network is composed of two RY gates, while the ANSATZ circuit is composed of a CNOT gate and two RX gates. The physical quantity to be measured is the Z operator acting on the No. 1 bit. +from projectq.ops import QubitOperator +============= +@circuit_generator(2) +def encoder(qubits): + RY('a') | qubits[0] + RY('b') | qubits[1] + +@circuit_generator(2) +def ansatz(qubits): + X | (qubits[0],qubits[1]) + RX('p1') | qubits[0] + RX('p2') | qubits[1] + +ham = mq.Hamiltonian(QubitOperator('Z1')) +encoder_names = ['a', 'b'] +ansatz_names = ['p1', 'p2'] +============= + +Here we generate an Encoder line and an Ansatz line using a decorator. The method of Generate PQC Operator is used to generate a circuit simulation +operator, and the simulation calculation of the quantum circuit is carried out,and the gradient value of the output of the quantum neural network to each parameter is obtained. In the Generate PQC Operator method, we need to provide the parameter name of the Encoder line, the parameter name of the Ansatz line, the entire quantum line, and the physical quantity to be measured. +============= +from mindquantum.nn import generate_pqc_operator +from mindspore import Tensor +from mindspore import context +context.set_context(mode=context.GRAPH_MODE, device_target="CPU") + +pqc = generate_pqc_operator(encoder_names, ansatz_names, encoder+ansatz, ham) +encoder_data = Tensor(np.array([[0.1,0.2]]).astype(np.float32)) +ansatz_data = Tensor(np.array([0.3,0.4]).astype(np.float32)) +measure_result, encoder_grad, ansatz_grad = pqc(encoder_data, ansatz_data) +print('Measurement result: ', measure_result.asnumpy()) +print('Gradient of encoder parameters: ', encoder_grad.asnumpy()) +print('Gradient of ansatz parameters: ', ansatz_grad.asnumpy()) +Measurement result: [[0.89819133]] +Gradient of encoder parameters: [[[-0.09011973 -0.1820724 ]]] +Gradient of ansatz parameters: [[[-2.7755576e-17 -3.7974921e-01]]] +============= +The above three results represent the output value of the quantum neural network, the gradient value of the parameters in the coded circuit and the gradient value of the parameters in the Ansatz circuit with training, respectively. Sometimes, the quantum neural network acts as the first layer of the whole quantum classical hybrid neural network, so we do not need to take the derivative of the gradient in the coded line. For such lines that do not require a gradient, the No Grad method can be used to specify that the quantum lines that do not need to calculate the gradient do not take the derivative. +============= +encoder.no_grad() +pqc = generate_pqc_operator(encoder_names, ansatz_names, encoder+ansatz, ham) +measure_result, encoder_grad, ansatz_grad = pqc(encoder_data, ansatz_data) +print('Measurement result: ', measure_result.asnumpy()) +print('Gradient of encoder parameters: ', encoder_grad.asnumpy()) +print('Gradient of ansatz parameters: ', ansatz_grad.asnumpy()) +Measurement result: [[0.89819133]] +Gradient of encoder parameters: [[[0. 0.]]] +Gradient of ansatz parameters: [[[-2.7755576e-17 -3.7974921e-01]]] +============= +As can be seen from above, the derivative of the coded line parameters in the quantum neural network is zero, and the derivative is not taken in the actual simulation calculation process + +[输入链接说明](http://gitee.com/mindspore/docs/issues/I3I2OD) \ No newline at end of file -- Gitee