QPanda3
Supported by OriginQ
Loading...
Searching...
No Matches
Variational Quantum Circuit

Prev Tutorial: PauliOperator
Next Tutorial: Quantum State


Introduction

QPanda3 uses VQCircuit to support variational quantum circuits. Provide placeholder support for setting variable parameters for logic gates in Ansatz using multidimensional arrays, support for generating QCircuit in batches based on multidimensional arrays, and support for calculating the expected values of Hamiltonians/Pauli operators for the generated QCircuit. In addition, VQCircuit also supports enabling the layer mechanism to better generate QCircuits composed of multiple quantum circuits with the same structure connected in series.

Batch Generation of QCircuit

Constructing a VQCircuit object

Here is API doc

Predefined Parameter Update Methods

Here is API doc for VQCircuit.set_Param

Constructing Ansatz

Here is API doc

Add QGate

Add the parameters of variable quantum logic gates with parameters one by one and fix them.

Add QCircuit

Add variable sub-circuit logic gates in batches, with parameters fixed for gates with parameters.

Add VQGate

Add variable quantum logic gates one by one, with parameters of the parameterized gates being variable.

Here is API doc for VQCircuit.Param

Show Ansatz

Display Ansatz related information.

Here is API doc for VQCircuit.display_ansatz

Parameters to QCircuits

Batch updating parameters in multidimensional arrays.

Here is API doc for apply params

Disable layer mechanism

Here is API doc for VQCircuit.disable_layer

Enable layer mechanism

Here is API doc for VQCircuit.enable_layer

Post-processing of Results

Obtain QCircuits

Obtaining QCircuits using multidimensional indexing.

Here is API doc for VQCResult.at

Calculate theoretical Hamiltonian expectation

Calculating theoretical expectation values of hamiltonians.

Batch process

Calculate the expected values of all QCircuits in batch.

Here is API doc for VQCResult.expval_hamiltonian

Please use VQCResult.expval_at to get every expectation val

Non-batch process

Calculate the expected non-batch processing method corresponding to a single QCircuit.

Here is API doc for VQCResult.expval_hamiltonian

Calculate Hamiltonian expectation with noise model

Calculating expectation values of hamiltonians with noise model

Batch process

Calculate the expected values of all QCircuits in batch.

Here is API doc for VQCResult.expval_hamiltonian

Please use VQCResult.expval_at to get every expectation val

Non-batch process

Calculate the expected non-batch processing method corresponding to a single QCircuit.

Here is API doc for VQCResult.expval_hamiltonian

Calculate theoretical Pauli Operators expectation

Calculating theoretical expectation values of pauli operators.

Batch process

Calculate the expected values of all QCircuits PauliOperators in batch.

Here is API doc for VQCResult.expval_pauli_operator Please use VQCResult.expval_at to get every expectation val

Non-batch process

Calculate the expected non-batch processing method corresponding to a single QCircuit pauli.

Here is API doc for VQCResult.expval_pauli_operator

Calculate Pauli Operators expectation with noise model

Calculating theoretical expectation values of pauli operators with noise model

Batch process

Calculate the expected values of all QCircuits PauliOperators in batch.

Here is API doc for VQCResult.expval_pauli_operator Please use VQCResult.expval_at to get every expectation val

Non-batch process

Calculate the expected non-batch processing method corresponding to a single QCircuit pauli.

Here is API doc for VQCResult.expval_pauli_operator

An Example

Output

---gates of a layer circuit:
___
gate id:0
gate type:H
qbits:0,
gate's params' total:0
gate's params:
___
gate id:1
gate type:X
qbits:0,
gate's params' total:0
gate's params:
___
gate id:2
gate type:CP
qbits:1,2,
gate's params' total:1
gate's params:
1th param is fixed. It's val is 3.14
___
gate id:3
gate type:Y
qbits:0,
gate's params' total:0
gate's params:
___
gate id:4
gate type:U2
qbits:1,
gate's params' total:2
gate's params:
1th param is fixed. It's val is 4.14
2th param is fixed. It's val is 5.14
___
gate id:5
gate type:RX
qbits:0,
gate's params' total:1
gate's params:
1th param is mutable.
It's pos in parameters is:0,0
It's label is:theta
___
gate id:6
gate type:RX
qbits:2,
gate's params' total:1
gate's params:
1th param is mutable.
It's pos in parameters is:0,0
It's label is:theta
___
gate id:7
gate type:RX
qbits:1,
gate's params' total:1
gate's params:
1th param is mutable.
It's pos in parameters is:1,0
It's label is:
___
gate id:8
gate type:RY
qbits:0,
gate's params' total:1
gate's params:
1th param is mutable.
It's pos in parameters is:0,1
It's label is:
___
gate id:9
gate type:U2
qbits:1,
gate's params' total:2
gate's params:
1th param is mutable.
It's pos in parameters is:1,1
It's label is:
2th param is fixed. It's val is 5.55
___
gate id:10
gate type:RY
qbits:2,
gate's params' total:1
gate's params:
1th param is mutable.
It's pos in parameters is:2,1
It's label is:
---
data.shape (3, 2, 3, 2)
data
[[[[1111. 1112.]
[1121. 1122.]
[1131. 1132.]]
[[1211. 1212.]
[1221. 1222.]
[1231. 1232.]]]
[[[2111. 2112.]
[2121. 2122.]
[2131. 2132.]]
[[2211. 2212.]
[2221. 2222.]
[2231. 2232.]]]
[[[3111. 3112.]
[3121. 3122.]
[3131. 3132.]]
[[3211. 3212.]
[3221. 3222.]
[3231. 3232.]]]]
┌─┐ ┌─┐ ┌─┐ ┌─────────────────┐ ┌─────────────────┐
q_0: |0>─┤H├───────────── ┤X├───────────────────────── ┤Y├──────────────── ┤RX(1111.00000000)├──────────── ┤RY(1112.00000000)├
└─┘ ├─┴────────────────────────┐ ├─┴───────────────┐ ├─────────────────┴───────────┐ └─────────────────┘
q_1: |0>─────────*─────── ┤U2(4.14000000, 5.14000000)├ ┤RX(1121.00000000)├ ┤U2(1122.00000000, 5.55000000)├ ───────────────────
┌───────┴──────┐ ├─────────────────┬────────┘ ├─────────────────┤ └─────────────────────────────┘
q_2: |0>─┤CP(3.14000000)├ ┤RX(1111.00000000)├───────── ┤RY(1132.00000000)├ ─────────────────────────────── ───────────────────
└──────────────┘ └─────────────────┘ └─────────────────┘
c : / ═
┌─┐ ┌─┐ ┌─┐ ┌─────────────────┐ ┌─────────────────┐
q_0: |0>─┤H├───────────── ┤X├───────────────────────── ┤Y├──────────────── ┤RX(1211.00000000)├──────────── ┤RY(1212.00000000)├
└─┘ ├─┴────────────────────────┐ ├─┴───────────────┐ ├─────────────────┴───────────┐ └─────────────────┘
q_1: |0>─────────*─────── ┤U2(4.14000000, 5.14000000)├ ┤RX(1221.00000000)├ ┤U2(1222.00000000, 5.55000000)├ ───────────────────
┌───────┴──────┐ ├─────────────────┬────────┘ ├─────────────────┤ └─────────────────────────────┘
q_2: |0>─┤CP(3.14000000)├ ┤RX(1211.00000000)├───────── ┤RY(1232.00000000)├ ─────────────────────────────── ───────────────────
└──────────────┘ └─────────────────┘ └─────────────────┘
c : / ═
┌─┐ ┌─┐ ┌─┐ ┌─────────────────┐ ┌─────────────────┐
q_0: |0>─┤H├───────────── ┤X├───────────────────────── ┤Y├──────────────── ┤RX(2111.00000000)├──────────── ┤RY(2112.00000000)├
└─┘ ├─┴────────────────────────┐ ├─┴───────────────┐ ├─────────────────┴───────────┐ └─────────────────┘
q_1: |0>─────────*─────── ┤U2(4.14000000, 5.14000000)├ ┤RX(2121.00000000)├ ┤U2(2122.00000000, 5.55000000)├ ───────────────────
┌───────┴──────┐ ├─────────────────┬────────┘ ├─────────────────┤ └─────────────────────────────┘
q_2: |0>─┤CP(3.14000000)├ ┤RX(2111.00000000)├───────── ┤RY(2132.00000000)├ ─────────────────────────────── ───────────────────
└──────────────┘ └─────────────────┘ └─────────────────┘
c : / ═
┌─┐ ┌─┐ ┌─┐ ┌─────────────────┐ ┌─────────────────┐
q_0: |0>─┤H├───────────── ┤X├───────────────────────── ┤Y├──────────────── ┤RX(2211.00000000)├──────────── ┤RY(2212.00000000)├
└─┘ ├─┴────────────────────────┐ ├─┴───────────────┐ ├─────────────────┴───────────┐ └─────────────────┘
q_1: |0>─────────*─────── ┤U2(4.14000000, 5.14000000)├ ┤RX(2221.00000000)├ ┤U2(2222.00000000, 5.55000000)├ ───────────────────
┌───────┴──────┐ ├─────────────────┬────────┘ ├─────────────────┤ └─────────────────────────────┘
q_2: |0>─┤CP(3.14000000)├ ┤RX(2211.00000000)├───────── ┤RY(2232.00000000)├ ─────────────────────────────── ───────────────────
└──────────────┘ └─────────────────┘ └─────────────────┘
c : / ═
┌─┐ ┌─┐ ┌─┐ ┌─────────────────┐ ┌─────────────────┐
q_0: |0>─┤H├───────────── ┤X├───────────────────────── ┤Y├──────────────── ┤RX(3111.00000000)├──────────── ┤RY(3112.00000000)├
└─┘ ├─┴────────────────────────┐ ├─┴───────────────┐ ├─────────────────┴───────────┐ └─────────────────┘
q_1: |0>─────────*─────── ┤U2(4.14000000, 5.14000000)├ ┤RX(3121.00000000)├ ┤U2(3122.00000000, 5.55000000)├ ───────────────────
┌───────┴──────┐ ├─────────────────┬────────┘ ├─────────────────┤ └─────────────────────────────┘
q_2: |0>─┤CP(3.14000000)├ ┤RX(3111.00000000)├───────── ┤RY(3132.00000000)├ ─────────────────────────────── ───────────────────
└──────────────┘ └─────────────────┘ └─────────────────┘
c : / ═
┌─┐ ┌─┐ ┌─┐ ┌─────────────────┐ ┌─────────────────┐
q_0: |0>─┤H├───────────── ┤X├───────────────────────── ┤Y├──────────────── ┤RX(3211.00000000)├──────────── ┤RY(3212.00000000)├
└─┘ ├─┴────────────────────────┐ ├─┴───────────────┐ ├─────────────────┴───────────┐ └─────────────────┘
q_1: |0>─────────*─────── ┤U2(4.14000000, 5.14000000)├ ┤RX(3221.00000000)├ ┤U2(3222.00000000, 5.55000000)├ ───────────────────
┌───────┴──────┐ ├─────────────────┬────────┘ ├─────────────────┤ └─────────────────────────────┘
q_2: |0>─┤CP(3.14000000)├ ┤RX(3211.00000000)├───────── ┤RY(3232.00000000)├ ─────────────────────────────── ───────────────────
└──────────────┘ └─────────────────┘ └─────────────────┘
c : / ═
------------------------------------
┌─┐ ┌─┐ ┌─┐ ┌─────────────────┐ >
q_0: |0>─┤H├───────────── ┤X├───────────────────────── ┤Y├──────────────── ┤RX(1111.00000000)├──────────── >
└─┘ ├─┴────────────────────────┐ ├─┴───────────────┐ ├─────────────────┴───────────┐ >
q_1: |0>─────────*─────── ┤U2(4.14000000, 5.14000000)├ ┤RX(1121.00000000)├ ┤U2(1122.00000000, 5.55000000)├ >
┌───────┴──────┐ ├─────────────────┬────────┘ ├─────────────────┤ └─────────────────────────────┘ >
q_2: |0>─┤CP(3.14000000)├ ┤RX(1111.00000000)├───────── ┤RY(1132.00000000)├ ─────────────────────────────── >
└──────────────┘ └─────────────────┘ └─────────────────┘ >
c : / ═
┌─────────────────┐ ┌─┐ ┌─┐ ┌─┐ ┌─────────────────┐ ┌─────────────────┐
q_0: |0>┤RY(1112.00000000)├ ┤H├───────────────────────── ┤X├──────────────── ┤Y├──────────────────────────── ┤RX(1211.00000000)├ ┤RY(1212.00000000)├
└─────────────────┘ ├─┴────────────────────────┐ ├─┴───────────────┐ ├─┴───────────────────────────┐ └─────────────────┘ └─────────────────┘
q_1: |0>────────*────────── ┤U2(4.14000000, 5.14000000)├ ┤RX(1221.00000000)├ ┤U2(1222.00000000, 5.55000000)├ ─────────────────── ───────────────────
┌───────┴──────┐ ├─────────────────┬────────┘ ├─────────────────┤ └─────────────────────────────┘
q_2: |0>┤CP(3.14000000)├─── ┤RX(1211.00000000)├───────── ┤RY(1232.00000000)├ ─────────────────────────────── ─────────────────── ───────────────────
└──────────────┘ └─────────────────┘ └─────────────────┘
c : /
┌─┐ ┌─┐ ┌─┐ ┌─────────────────┐ >
q_0: |0>─┤H├───────────── ┤X├───────────────────────── ┤Y├──────────────── ┤RX(2111.00000000)├──────────── >
└─┘ ├─┴────────────────────────┐ ├─┴───────────────┐ ├─────────────────┴───────────┐ >
q_1: |0>─────────*─────── ┤U2(4.14000000, 5.14000000)├ ┤RX(2121.00000000)├ ┤U2(2122.00000000, 5.55000000)├ >
┌───────┴──────┐ ├─────────────────┬────────┘ ├─────────────────┤ └─────────────────────────────┘ >
q_2: |0>─┤CP(3.14000000)├ ┤RX(2111.00000000)├───────── ┤RY(2132.00000000)├ ─────────────────────────────── >
└──────────────┘ └─────────────────┘ └─────────────────┘ >
c : / ═
┌─────────────────┐ ┌─┐ ┌─┐ ┌─┐ ┌─────────────────┐ ┌─────────────────┐
q_0: |0>┤RY(2112.00000000)├ ┤H├───────────────────────── ┤X├──────────────── ┤Y├──────────────────────────── ┤RX(2211.00000000)├ ┤RY(2212.00000000)├
└─────────────────┘ ├─┴────────────────────────┐ ├─┴───────────────┐ ├─┴───────────────────────────┐ └─────────────────┘ └─────────────────┘
q_1: |0>────────*────────── ┤U2(4.14000000, 5.14000000)├ ┤RX(2221.00000000)├ ┤U2(2222.00000000, 5.55000000)├ ─────────────────── ───────────────────
┌───────┴──────┐ ├─────────────────┬────────┘ ├─────────────────┤ └─────────────────────────────┘
q_2: |0>┤CP(3.14000000)├─── ┤RX(2211.00000000)├───────── ┤RY(2232.00000000)├ ─────────────────────────────── ─────────────────── ───────────────────
└──────────────┘ └─────────────────┘ └─────────────────┘
c : /
┌─┐ ┌─┐ ┌─┐ ┌─────────────────┐ >
q_0: |0>─┤H├───────────── ┤X├───────────────────────── ┤Y├──────────────── ┤RX(3111.00000000)├──────────── >
└─┘ ├─┴────────────────────────┐ ├─┴───────────────┐ ├─────────────────┴───────────┐ >
q_1: |0>─────────*─────── ┤U2(4.14000000, 5.14000000)├ ┤RX(3121.00000000)├ ┤U2(3122.00000000, 5.55000000)├ >
┌───────┴──────┐ ├─────────────────┬────────┘ ├─────────────────┤ └─────────────────────────────┘ >
q_2: |0>─┤CP(3.14000000)├ ┤RX(3111.00000000)├───────── ┤RY(3132.00000000)├ ─────────────────────────────── >
└──────────────┘ └─────────────────┘ └─────────────────┘ >
c : / ═
┌─────────────────┐ ┌─┐ ┌─┐ ┌─┐ ┌─────────────────┐ ┌─────────────────┐
q_0: |0>┤RY(3112.00000000)├ ┤H├───────────────────────── ┤X├──────────────── ┤Y├──────────────────────────── ┤RX(3211.00000000)├ ┤RY(3212.00000000)├
└─────────────────┘ ├─┴────────────────────────┐ ├─┴───────────────┐ ├─┴───────────────────────────┐ └─────────────────┘ └─────────────────┘
q_1: |0>────────*────────── ┤U2(4.14000000, 5.14000000)├ ┤RX(3221.00000000)├ ┤U2(3222.00000000, 5.55000000)├ ─────────────────── ───────────────────
┌───────┴──────┐ ├─────────────────┬────────┘ ├─────────────────┤ └─────────────────────────────┘
q_2: |0>┤CP(3.14000000)├─── ┤RX(3211.00000000)├───────── ┤RY(3232.00000000)├ ─────────────────────────────── ─────────────────── ───────────────────
└──────────────┘ └─────────────────┘ └─────────────────┘
c : /
expectation: -1.928947450418577
expectation: -1.928947450418577
-1.2142270395620407
0.12937804539398867
-1.9289474504185768
-1.2142270395620407
0.12937804539398867
-1.9289474504185768
---gates of a layer circuit:
___
gate id:0
gate type:H
qbits:0,
gate's params' total:0
gate's params:
___
gate id:1
gate type:X
qbits:0,
gate's params' total:0
gate's params:
___
gate id:2
gate type:CP
qbits:1,2,
gate's params' total:1
gate's params:
1th param is fixed. It's val is 3.14
___
gate id:3
gate type:Y
qbits:0,
gate's params' total:0
gate's params:
___
gate id:4
gate type:U2
qbits:1,
gate's params' total:2
gate's params:
1th param is fixed. It's val is 4.14
2th param is fixed. It's val is 5.14
___
gate id:5
gate type:RX
qbits:0,
gate's params' total:1
gate's params:
1th param is mutable.
It's pos in parameters is:0,0
It's label is:theta
___
gate id:6
gate type:RX
qbits:2,
gate's params' total:1
gate's params:
1th param is mutable.
It's pos in parameters is:0,0
It's label is:theta
___
gate id:7
gate type:RX
qbits:1,
gate's params' total:1
gate's params:
1th param is mutable.
It's pos in parameters is:1,0
It's label is:
___
gate id:8
gate type:RY
qbits:0,
gate's params' total:1
gate's params:
1th param is mutable.
It's pos in parameters is:0,1
It's label is:
___
gate id:9
gate type:U2
qbits:1,
gate's params' total:2
gate's params:
1th param is mutable.
It's pos in parameters is:1,1
It's label is:
2th param is fixed. It's val is 5.55
___
gate id:10
gate type:RY
qbits:2,
gate's params' total:1
gate's params:
1th param is mutable.
It's pos in parameters is:2,1
It's label is:
---
dims: [3, 2]
┌─┐ ┌─┐ ┌─┐ ┌───────────────┐ ┌───────────────┐
q_0: |0>─┤H├───────────── ┤X├───────────────────────── ┤Y├────────────── ┤RX(11.00000000)├──────────── ┤RY(12.00000000)├
└─┘ ├─┴────────────────────────┐ ├─┴─────────────┐ ├───────────────┴───────────┐ └───────────────┘
q_1: |0>─────────*─────── ┤U2(4.14000000, 5.14000000)├ ┤RX(21.00000000)├ ┤U2(22.00000000, 5.55000000)├ ─────────────────
┌───────┴──────┐ ├───────────────┬──────────┘ ├───────────────┤ └───────────────────────────┘
q_2: |0>─┤CP(3.14000000)├ ┤RX(11.00000000)├─────────── ┤RY(32.00000000)├ ───────────────────────────── ─────────────────
└──────────────┘ └───────────────┘ └───────────────┘
c : / ═