Skip to content

Commit

Permalink
Revert "update: Re-enable commented code chunks in several notebooks … (
Browse files Browse the repository at this point in the history
  • Loading branch information
ajberdy authored Oct 16, 2023
1 parent 0af523b commit 2b57395
Show file tree
Hide file tree
Showing 19 changed files with 892 additions and 2,412 deletions.
424 changes: 199 additions & 225 deletions examples/braket_features/Using_the_tensor_network_simulator_TN1.ipynb

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,9 @@
"source": [
"import pennylane as qml\n",
"from pennylane import numpy as np\n",
"from braket.devices import Devices\n",
"\n",
"wires = 25\n",
"device_arn = Devices.Amazon.SV1"
"device_arn = \"arn:aws:braket:::device/quantum-simulator/amazon/sv1\""
]
},
{
Expand Down Expand Up @@ -253,8 +252,8 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Execution time on remote device (seconds): 7.802760124206543\n",
"Execution time on local device (seconds): 28.442059993743896\n"
"Execution time on remote device (seconds): 3.8534095287323\n",
"Execution time on local device (seconds): 16.776463270187378\n"
]
}
],
Expand Down Expand Up @@ -285,9 +284,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Now let us compare the gradient-calculation times between the two devices. Remember that when loading the remote device, we set ``parallel=True``. This allows the multiple device executions required during gradient calculations to be performed in parallel on SV1, so we expect the remote device to be much faster.\n",
"\n",
"To ensure that the runtime on the local simulator is not excessive, we will demonstrate this comparison using a smaller circuit with only 22 qubits."
"Now let us compare the gradient-calculation times between the two devices. Remember that when loading the remote device, we set ``parallel=True``. This allows the multiple device executions required during gradient calculations to be performed in parallel on SV1, so we expect the remote device to be much faster."
]
},
{
Expand All @@ -296,27 +293,6 @@
"metadata": {},
"outputs": [],
"source": [
"wires = 22\n",
"\n",
"dev_remote = qml.device(\n",
" \"braket.aws.qubit\",\n",
" device_arn=device_arn,\n",
" wires=wires,\n",
" parallel=True,\n",
")\n",
"\n",
"dev_local = qml.device(\"braket.local.qubit\", wires=wires)\n",
"\n",
"def circuit(params):\n",
" for i in range(wires):\n",
" qml.RX(params[i], wires=i)\n",
" for i in range(wires):\n",
" qml.CNOT(wires=[i, (i + 1) % wires])\n",
" return qml.expval(qml.PauliZ(wires - 1))\n",
"\n",
"qnode_remote = qml.QNode(circuit, dev_remote)\n",
"qnode_local = qml.QNode(circuit, dev_local)\n",
"\n",
"d_qnode_remote = qml.grad(qnode_remote)\n",
"d_qnode_local = qml.grad(qnode_local)"
]
Expand All @@ -325,39 +301,69 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"The following cell will result in 45 circuits being executed (in parallel) on SV1. We must execute the circuit twice to evaluate the partial derivative with respect to each parameter. Hence, for 22 parameters there are 44 circuit executions. The final circuit execution is due to a \"forward pass\" evaluation of the QNode before the gradient is calculated."
"The following cell will result in 51 circuits being executed (in parallel) on SV1. We must execute the circuit twice to evaluate the partial derivative with respect to each parameter. Hence, for 25 parameters there are 50 circuit executions. The final circuit execution is due to a \"forward pass\" evaluation of the QNode before the gradient is calculated."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"t_0_remote_grad = time.time()\n",
"\n",
"d_qnode_remote(params)\n",
"\n",
"t_1_remote_grad = time.time()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-block alert-warning\">\n",
"<b>Caution:</b> Depending on your hardware, running the following cell can take 15 minutes or longer. Only uncomment it if you are happy to wait.\n",
"</div>"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"# t_0_local_grad = time.time()\n",
"\n",
"# d_qnode_local(params)\n",
"\n",
"# t_1_local_grad = time.time()"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Gradient calculation time on remote device (seconds): 4.199454069137573\n"
"Gradient calculation time on remote device (seconds): 29.94787311553955\n"
]
}
],
"source": [
"t_0_remote_grad = time.time()\n",
"\n",
"d_qnode_remote(params)\n",
"\n",
"t_1_remote_grad = time.time()\n",
"\n",
"print(\"Gradient calculation time on remote device (seconds):\", t_1_remote_grad - t_0_remote_grad)"
"print(\"Gradient calculation time on remote device (seconds):\", t_1_remote_grad - t_0_remote_grad)\n",
"# print(\"Gradient calculation time on local device (seconds):\", t_1_local_grad - t_0_local_grad)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Gradient calculation on SV1 takes less than 10 seconds. For comparison, if you had run `d_qnode_local(params)` to calculate the gradient on the local device, you would have seen times of around 5 minutes or more! This provides a powerful lesson in parallelization.\n",
"If you had the patience to run the local device, you will see times of around 15 minutes or more! Compare this to less than a minute spent calculating the gradient on SV1. This provides a powerful lesson in parallelization.\n",
"\n",
"What if we had run on SV1 with ``parallel=False``? It would have taken around 1 minute—still faster than a local device, but much slower than running SV1 in parallel."
"What if we had run on SV1 with ``parallel=False``? It would have taken around 3 minutes—still faster than a local device, but much slower than running SV1 in parallel."
]
},
{
Expand All @@ -372,17 +378,17 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Quantum Task Summary\n",
"{<_Amazon.SV1: 'arn:aws:braket:::device/quantum-simulator/amazon/sv1'>: {'shots': 0, 'tasks': {'COMPLETED': 2}, 'execution_duration': datetime.timedelta(seconds=3, microseconds=885000), 'billed_execution_duration': datetime.timedelta(seconds=6, microseconds=468000)}}\n",
"Task Summary\n",
"{'arn:aws:braket:::device/quantum-simulator/amazon/sv1': {'shots': 0, 'tasks': {'COMPLETED': 52}, 'execution_duration': datetime.timedelta(seconds=18, microseconds=353000), 'billed_execution_duration': datetime.timedelta(seconds=156)}}\n",
"Note: Charges shown are estimates based on your Amazon Braket simulator and quantum processing unit (QPU) task usage. Estimated charges shown may differ from your actual charges. Estimated charges do not factor in any discounts or credits, and you may experience additional charges based on your use of other services such as Amazon Elastic Compute Cloud (Amazon EC2).\n",
"Estimated cost to run this example: 0.008 USD\n"
"Estimated cost to run this example: 0.195 USD\n"
]
}
],
Expand All @@ -396,7 +402,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3.8.10 ('venv': venv)",
"language": "python",
"name": "python3"
},
Expand All @@ -410,7 +416,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.4"
"version": "3.8.10"
},
"vscode": {
"interpreter": {
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,6 @@ def pre_run_inject(mock_utils):
mocker = mock_utils.Mocker()
mock_utils.mock_default_device_calls(mocker)
mocker.set_task_result_return(mock_utils.read_file("1_pennylane_results.json", __file__))
mocker.set_get_device_result({
"deviceArn": "arn:aws:braket:::device/quantum-simulator/amazon/sv1",
"deviceCapabilities": mock_utils.read_file("1_pennylane_capabilities.json", __file__),
"deviceName": "SV1",
"deviceQueueInfo": [
{
"queue": "QUANTUM_TASKS_QUEUE",
"queuePriority": "Normal",
"queueSize": "0"
},
{
"queue": "QUANTUM_TASKS_QUEUE",
"queuePriority": "Priority",
"queueSize": "0"
},
{
"queue": "JOBS_QUEUE",
"queueSize": "0"
}
],
"deviceStatus": "ONLINE",
"deviceType": "SIMULATOR",
"providerName": "Amazon Braket"
})


def post_run(tb):
Expand Down
Loading

0 comments on commit 2b57395

Please sign in to comment.