Skip to content

Commit

Permalink
Hello update (tensorflow#156)
Browse files Browse the repository at this point in the history
* Modified Hello Many worlds tutorial to verify outputs.

* metadata fix.

* last of metadata.

* Billy feedback.

* break things down.

* removed the.

* Update docs/tutorials/hello_many_worlds.ipynb

Co-Authored-By: Billy Lamberta <[email protected]>

* Removed blank cell at end.

Co-authored-by: Michael Broughton <[email protected]>
Co-authored-by: Billy Lamberta <[email protected]>
  • Loading branch information
3 people authored Mar 11, 2020
1 parent b75e063 commit c40af6b
Showing 1 changed file with 85 additions and 6 deletions.
91 changes: 85 additions & 6 deletions docs/tutorials/hello_many_worlds.ipynb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "hello_many_worlds.ipynb",
Expand All @@ -25,8 +27,6 @@
"version": "3.7.5rc1"
}
},
"nbformat": 4,
"nbformat_minor": 0,
"cells": [
{
"cell_type": "markdown",
Expand Down Expand Up @@ -793,11 +793,13 @@
},
"source": [
"random_rotations = np.random.uniform(0, 2 * np.pi, 3)\n",
"noisy_preparation = cirq.Circuit(\n",
" cirq.rx(random_rotations[0])(qubit),\n",
" cirq.ry(random_rotations[1])(qubit),\n",
" cirq.rz(random_rotations[2])(qubit)\n",
")\n",
"datapoint_circuits = tfq.convert_to_tensor([\n",
" cirq.Circuit(\n",
" cirq.rx(random_rotations[0])(qubit),\n",
" cirq.ry(random_rotations[1])(qubit),\n",
" cirq.rz(random_rotations[2])(qubit))\n",
" noisy_preparation\n",
"] * 2) # Make two copied of this circuit"
],
"execution_count": 0,
Expand Down Expand Up @@ -915,6 +917,73 @@
"From this plot you can see that the neural network has learned to overcome the systematic miscalibration."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "C2RfWismj66S",
"colab_type": "text"
},
"source": [
"### 2.6 Verify outputs\n",
"Now use the trained model, to correct the qubit calibration errors. With Cirq:"
]
},
{
"cell_type": "code",
"metadata": {
"id": "RoIlb7r7j5SY",
"colab_type": "code",
"colab": {}
},
"source": [
"def check_error(command_values, desired_values):\n",
" \"\"\"Based on the value in `command_value` see how well you could prepare\n",
" the full circuit to have `desired_value` when taking expectation w.r.t. Z.\"\"\"\n",
" params_to_prepare_output = controller(command_values).numpy()\n",
" full_circuit = noisy_preparation + model_circuit\n",
"\n",
" # Test how well you can prepare a state to get expectation the expectation\n",
" # value in `desired_values`\n",
" for index in [0, 1]:\n",
" state = cirq_simulator.simulate(\n",
" full_circuit,\n",
" {s:v for (s,v) in zip(control_params, params_to_prepare_output[index])}\n",
" ).final_state\n",
" expectation = z0.expectation_from_wavefunction(state, {qubit: 0}).real\n",
" print(f'For a desired output (expectation) of {desired_values[index]} with'\n",
" f' noisy preparation, the controller\\nnetwork found the following '\n",
" f'values for theta: {params_to_prepare_output[index]}\\nWhich gives an'\n",
" f' actual expectation of: {expectation}\\n')\n",
"\n",
"\n",
"check_error(commands, expected_outputs)"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "wvW_ZDwmsws6",
"colab_type": "text"
},
"source": [
"The value of the loss function during training provides a rough idea of how well the model is learning. The lower the loss, the closer the expectation values in the above cell is to `desired_values`. If you aren't as concerned with the parameter values, you can always check the outputs from above using `tfq`:"
]
},
{
"cell_type": "code",
"metadata": {
"id": "aYskLTacs8Ku",
"colab_type": "code",
"colab": {}
},
"source": [
"model([datapoint_circuits, commands])"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
Expand Down Expand Up @@ -1144,6 +1213,16 @@
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "n2WtXnsxubD2",
"colab_type": "text"
},
"source": [
"Success: See if you can adapt the `check_error` function from your first model to work with this new model architecture."
]
}
]
}

0 comments on commit c40af6b

Please sign in to comment.