Skip to content

Commit

Permalink
ucc derivative
Browse files Browse the repository at this point in the history
  • Loading branch information
Cryoris committed Sep 10, 2020
1 parent 52f5099 commit 471a387
Showing 1 changed file with 128 additions and 0 deletions.
128 changes: 128 additions & 0 deletions UCC.ipynb
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: &quot;Courier New&quot;,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
}

0 comments on commit 471a387

Please sign in to comment.