Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
erick-xanadu committed Aug 13, 2024
1 parent ee9c008 commit 8baa687
Showing 1 changed file with 42 additions and 31 deletions.
73 changes: 42 additions & 31 deletions demos/adaptive_circuits_demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"\n",
"import jax.numpy as jnp\n",
"\n",
"import functools\n",
"import warnings\n",
"warnings.filterwarnings('ignore')"
]
Expand Down Expand Up @@ -150,13 +151,21 @@
"# Define the device, using lightning.qubit device\n",
"dev = qml.device(\"lightning.qubit\", wires=qubits)\n",
"\n",
"def stopping_condition(obj):\n",
" return obj.name not in [\"BasisState\"]\n",
"\n",
"def decompose(stopping_condition, func):\n",
" return qml.devices.preprocess.decompose(func, stopping_condition, skip_initial_state_prep=False)\n",
"\n",
"@functools.partial(decompose, stopping_condition)\n",
"@qml.qnode(dev, diff_method=\"adjoint\")\n",
"def cost_func(params):\n",
" qml.RX(0.0, wires=[0])\n",
" qml.BasisState(hf, wires=range(qubits))\n",
" qml.DoubleExcitation(params[0], wires=[0, 1, 2, 3])\n",
" qml.DoubleExcitation(params[1], wires=[0, 1, 4, 5])\n",
" return qml.expval(hamiltonian)"
" return qml.expval(hamiltonian)\n",
"\n",
"cost_func(np.array([0.0, 0.0]))"
]
},
{
Expand Down Expand Up @@ -238,6 +247,7 @@
"hf = qml.qchem.hf_state(electrons=2, orbitals=6)\n",
"print(f\"The Hartree-Fock State: {hf}\")\n",
"\n",
"@functools.partial(decompose, stopping_condition)\n",
"@qml.qnode(qml.device(\"lightning.qubit\", wires=qubits))\n",
"def catalyst_cost_func(params):\n",
" qml.RX(0.0, wires=[0])\n",
Expand Down Expand Up @@ -573,30 +583,29 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Excitation: [0, 1, 2, 3], Gradient: -0.012782168180745743\n",
"Excitation: [0, 1, 2, 3], Gradient: -0.012782168180745672\n",
"Excitation: [0, 1, 2, 5], Gradient: 0.0\n",
"Excitation: [0, 1, 2, 7], Gradient: 0.0\n",
"Excitation: [0, 1, 2, 9], Gradient: 0.03426450359905824\n",
"Excitation: [0, 1, 2, 9], Gradient: 0.03426450359905814\n",
"Excitation: [0, 1, 3, 4], Gradient: 0.0\n",
"Excitation: [0, 1, 3, 6], Gradient: 0.0\n",
"Excitation: [0, 1, 3, 8], Gradient: -0.034264503599058235\n",
"Excitation: [0, 1, 4, 5], Gradient: -0.023581524379111044\n",
"Excitation: [0, 1, 3, 8], Gradient: -0.03426450359905814\n",
"Excitation: [0, 1, 4, 5], Gradient: -0.02358152437911096\n",
"Excitation: [0, 1, 4, 7], Gradient: 0.0\n",
"Excitation: [0, 1, 4, 9], Gradient: 0.0\n",
"Excitation: [0, 1, 5, 6], Gradient: 0.0\n",
"Excitation: [0, 1, 5, 8], Gradient: 0.0\n",
"Excitation: [0, 1, 6, 7], Gradient: -0.02358152437912195\n",
"Excitation: [0, 1, 6, 7], Gradient: -0.023581524379121893\n",
"Excitation: [0, 1, 6, 9], Gradient: 0.0\n",
"Excitation: [0, 1, 7, 8], Gradient: 0.0\n",
"Excitation: [0, 1, 8, 9], Gradient: -0.12362273289626559\n"
"Excitation: [0, 1, 8, 9], Gradient: -0.12362273289626545\n"
]
}
],
"source": [
"# Create a circuit that applies a selected group of gates\n",
"# to a reference Hartree-Fock state.\n",
"def circuit_1(params, excitations):\n",
" qml.RX(0.0, wires=[0])\n",
" qml.BasisState(hf, wires=range(qubits))\n",
"\n",
" for i, excitation in enumerate(excitations):\n",
Expand All @@ -609,6 +618,7 @@
"# Define the device and the cost function.\n",
"dev = qml.device(\"lightning.qubit\", wires=qubits)\n",
"cost_fn = qml.QNode(circuit_1, dev, diff_method=\"adjoint\")\n",
"cost_fn = decompose(stopping_condition, cost_fn)\n",
"\n",
"circuit_gradient = qml.grad(cost_fn, argnum=0)\n",
"\n",
Expand Down Expand Up @@ -708,14 +718,14 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Excitation : [0, 2], Gradient: -0.005062544629162063\n",
"Excitation : [0, 2], Gradient: 0.005062544629162047\n",
"Excitation : [0, 4], Gradient: 0.0\n",
"Excitation : [0, 6], Gradient: 0.0\n",
"Excitation : [0, 8], Gradient: -0.0009448055879247612\n",
"Excitation : [1, 3], Gradient: 0.004926625112976861\n",
"Excitation : [0, 8], Gradient: 0.0009448055879244589\n",
"Excitation : [1, 3], Gradient: -0.004926625112976867\n",
"Excitation : [1, 5], Gradient: 0.0\n",
"Excitation : [1, 7], Gradient: 0.0\n",
"Excitation : [1, 9], Gradient: 0.0014535553867889794\n",
"Excitation : [1, 9], Gradient: -0.0014535553867887066\n",
"singles_select: [[0, 2], [0, 8], [1, 3], [1, 9]]\n"
]
}
Expand All @@ -726,7 +736,6 @@
"# those that have a non-negligible gradient.\n",
"# Repeat steps 1 and 2 for the single excitations.\n",
"def circuit_2(params, excitations, gates_select, params_select):\n",
" qml.RX(0.0, wires=[0])\n",
" qml.BasisState(hf, wires=range(qubits))\n",
"\n",
" for i, gate in enumerate(gates_select):\n",
Expand All @@ -743,6 +752,7 @@
" return qml.expval(hamiltonian)\n",
"\n",
"cost_fn = qml.QNode(circuit_2, dev, diff_method=\"adjoint\")\n",
"cost_fn = decompose(stopping_condition, cost_fn)\n",
"circuit_gradient = qml.grad(cost_fn, argnum=0)\n",
"params = [0.0] * len(singles)\n",
"\n",
Expand Down Expand Up @@ -824,6 +834,7 @@
"metadata": {},
"outputs": [],
"source": [
"@functools.partial(decompose, stopping_condition)\n",
"@qml.qnode(qml.device(\"lightning.qubit\", wires=qubits))\n",
"def catalyst_avqe_circuit(\n",
" num_gates, num_selected_gates,\n",
Expand Down Expand Up @@ -889,22 +900,22 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Excitation: [0 1 2 3], Gradient: -0.012782148672840776\n",
"Excitation: [0 1 2 5], Gradient: 1.7763568394002505e-08\n",
"Excitation: [0 1 2 7], Gradient: 1.7763568394002505e-08\n",
"Excitation: [0 1 2 9], Gradient: 0.034264520110127705\n",
"Excitation: [0 1 3 4], Gradient: 1.7763568394002505e-08\n",
"Excitation: [0 1 3 6], Gradient: 1.7763568394002505e-08\n",
"Excitation: [0 1 3 8], Gradient: -0.03426448458299092\n",
"Excitation: [0 1 4 5], Gradient: -0.023581492314406205\n",
"Excitation: [0 1 4 7], Gradient: 1.7763568394002505e-08\n",
"Excitation: [0 1 4 9], Gradient: 1.7763568394002505e-08\n",
"Excitation: [0 1 5 6], Gradient: 1.7763568394002505e-08\n",
"Excitation: [0 1 5 8], Gradient: 1.7763568394002505e-08\n",
"Excitation: [0 1 6 7], Gradient: -0.023581501196190402\n",
"Excitation: [0 1 6 9], Gradient: 8.881784197001252e-09\n",
"Excitation: [0 1 7 8], Gradient: 8.881784197001252e-09\n",
"Excitation: [0 1 8 9], Gradient: -0.1236227031853332\n"
"Excitation: [0 1 2 3], Gradient: -0.012782095382135594\n",
"Excitation: [0 1 2 5], Gradient: 7.105427357601002e-08\n",
"Excitation: [0 1 2 7], Gradient: 7.105427357601002e-08\n",
"Excitation: [0 1 2 9], Gradient: 0.03426457340083289\n",
"Excitation: [0 1 3 4], Gradient: 7.105427357601002e-08\n",
"Excitation: [0 1 3 6], Gradient: 9.769962616701378e-08\n",
"Excitation: [0 1 3 8], Gradient: -0.034264404646933144\n",
"Excitation: [0 1 4 5], Gradient: -0.023581412378348432\n",
"Excitation: [0 1 4 7], Gradient: 9.769962616701378e-08\n",
"Excitation: [0 1 4 9], Gradient: 7.105427357601002e-08\n",
"Excitation: [0 1 5 6], Gradient: 7.105427357601002e-08\n",
"Excitation: [0 1 5 8], Gradient: 7.105427357601002e-08\n",
"Excitation: [0 1 6 7], Gradient: -0.023581412378348432\n",
"Excitation: [0 1 6 9], Gradient: 6.217248937900877e-08\n",
"Excitation: [0 1 7 8], Gradient: 6.217248937900877e-08\n",
"Excitation: [0 1 8 9], Gradient: -0.12362264989462801\n"
]
}
],
Expand Down Expand Up @@ -991,7 +1002,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"params_doubles: [ 0.04758599 -0.09855459 0.09865966 0.05330077 0.05331789 0.22938115]\n"
"params_doubles: [ 0.04758601 -0.09855455 0.09865969 0.0533007 0.0533178 0.22938115]\n"
]
}
],
Expand Down

0 comments on commit 8baa687

Please sign in to comment.