-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 33, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/html": [ | ||
"<pre style=\"word-wrap: normal;white-space: pre;background: #fff0;line-height: 1.1;font-family: "Courier New",Courier,monospace\"> ┌───┐┌────────────────────┐┌────────────────────┐┌────────────────────┐\n", | ||
"q_0: ┤ X ├┤0 ├┤0 ├┤0 ├\n", | ||
" └───┘│ ││ ││ │\n", | ||
"q_1: ─────┤1 ├┤1 ├┤1 ├\n", | ||
" │ Evolution^1(x[0]) ││ Evolution^1(x[1]) ││ Evolution^1(x[2]) │\n", | ||
"q_2: ─────┤2 ├┤2 ├┤2 ├\n", | ||
" │ ││ ││ │\n", | ||
"q_3: ─────┤3 ├┤3 ├┤3 ├\n", | ||
" └────────────────────┘└────────────────────┘└────────────────────┘</pre>" | ||
], | ||
"text/plain": [ | ||
" ┌───┐┌────────────────────┐┌────────────────────┐┌────────────────────┐\n", | ||
"q_0: ┤ X ├┤0 ├┤0 ├┤0 ├\n", | ||
" └───┘│ ││ ││ │\n", | ||
"q_1: ─────┤1 ├┤1 ├┤1 ├\n", | ||
" │ Evolution^1(x[0]) ││ Evolution^1(x[1]) ││ Evolution^1(x[2]) │\n", | ||
"q_2: ─────┤2 ├┤2 ├┤2 ├\n", | ||
" │ ││ ││ │\n", | ||
"q_3: ─────┤3 ├┤3 ├┤3 ├\n", | ||
" └────────────────────┘└────────────────────┘└────────────────────┘" | ||
] | ||
}, | ||
"execution_count": 33, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"import numpy as np\n", | ||
"from qiskit.quantum_info import Statevector\n", | ||
"from qiskit.circuit import ParameterVector\n", | ||
"from qiskit.chemistry.components.variational_forms import UCCSD\n", | ||
"\n", | ||
"x = ParameterVector('x', 3)\n", | ||
"uvcc_varform = UCCSD(4, 2, qubit_mapping = 'jordan_wigner', two_qubit_reduction=False)\n", | ||
"circuit = QuantumCircuit(4)\n", | ||
"circuit.x(0) # initial state\n", | ||
"circuit.compose(uvcc_varform.construct_circuit(x), inplace=True) # add UCC ansatz\n", | ||
"circuit.draw()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 42, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# param shift rule\n", | ||
"finite_difference = True \n", | ||
"shift = 0.1\n", | ||
"gradient_circuits = {}\n", | ||
"point = {x_i: np.random.random(1)[0] for x_i in x}\n", | ||
"for x_i in x:\n", | ||
" # shifted parameters\n", | ||
" plus = circuit.bind_parameters({x_j: point_j + shift * int(x_i == x_j) for x_j, point_j in point.items()})\n", | ||
" minus = circuit.bind_parameters({x_j: point_j - shift * int(x_i == x_j) for x_j, point_j in point.items()})\n", | ||
" gradient_circuits[x_i] = (plus, minus)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 44, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"x[0] [-0. +0.j -0.02+0.j 1. -0.j -0. +0.j 0. -0.j -0. +0.j -0. +0.j\n", | ||
" -0. -0.j -0. +0.j -0. +0.j -0. +0.j 0. -0.j 0. -0.j -0. -0.j\n", | ||
" 0. +0.j -0. -0.j]\n", | ||
"x[1] [ 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\n", | ||
" -0.+0.j -0.-0.j -0.+0.j -0.-0.j -0.+0.j 0.+0.j 0.+0.j]\n", | ||
"x[2] [-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\n", | ||
" 0.+0.j -0.-0.j -0.+0.j 0.+0.j 0.-0.j 0.+0.j -0.+0.j]\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"for x_i, (plus, minus) in gradient_circuits.items():\n", | ||
" # print(plus.draw())\n", | ||
" # print(minus.draw())\n", | ||
" difference = Statevector.from_instruction(plus) - Statevector.from_instruction(minus)\n", | ||
" if finite_difference:\n", | ||
" difference /= 2 * shift\n", | ||
" print(x_i, np.round(difference.data, 2))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3.7.5 64-bit ('latest': virtualenv)", | ||
"language": "python", | ||
"name": "python37564bitlatestvirtualenv76cfe9d7d3f749489cf2ff69593234af" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.7.5" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |