Skip to content

Commit

Permalink
Lint example notebooks
Browse files Browse the repository at this point in the history
  • Loading branch information
rmshaffer committed May 7, 2024
1 parent 79020a8 commit 7e2a770
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 22 deletions.
2 changes: 1 addition & 1 deletion examples/2_Expressing_classical_control_flow.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@
" else:\n",
" bell(3, 4)\n",
"\n",
" return measure()\n"
" return measure()"
]
},
{
Expand Down
1 change: 1 addition & 0 deletions examples/3_1_Iterative_phase_estimation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
"source": [
"n_iterations = 4\n",
"\n",
"\n",
"@aq.main(num_qubits=2)\n",
"def ipe():\n",
" \"\"\"Iterative phase estimation algorithm.\"\"\"\n",
Expand Down
21 changes: 11 additions & 10 deletions examples/3_2_magic_state_distillation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,12 @@
"@aq.subroutine\n",
"def physical_magic_state_a_type(q: int):\n",
" ins.h(q)\n",
" ins.rz(q, np.pi/6)\n",
" ins.rz(q, np.pi / 6)\n",
"\n",
"\n",
"@aq.subroutine\n",
"def basis_rotation_pi6(q: int):\n",
" ins.rz(q, -np.pi/6)\n",
" ins.rz(q, -np.pi / 6)\n",
" ins.h(q)"
]
},
Expand Down Expand Up @@ -146,7 +147,7 @@
" new_probs[new_bitstring] += value\n",
"\n",
" total_shots = sum(new_probs.values())\n",
" return {k:v/total_shots for k,v in new_probs.items()}"
" return {k: v / total_shots for k, v in new_probs.items()}"
]
},
{
Expand Down Expand Up @@ -179,7 +180,7 @@
"print(\"measurement counts: \", counts)\n",
"\n",
"# Post-select the measurement outcome that measures \"0\" in ancilla\n",
"post_selected_counts = {k:v for k,v in counts.items() if k[1]==\"0\"}\n",
"post_selected_counts = {k: v for k, v in counts.items() if k[1] == \"0\"}\n",
"\n",
"# Compute the expectation value of Z observable on the data qubit\n",
"marginal_probs = get_marginal_probs(post_selected_counts, [0])\n",
Expand Down Expand Up @@ -213,8 +214,8 @@
"source": [
"@aq.subroutine\n",
"def physical_magic_state_t_type(q: int):\n",
" ins.ry(q, np.arccos(1/np.sqrt(3)))\n",
" ins.rz(q, np.pi/4)"
" ins.ry(q, np.arccos(1 / np.sqrt(3)))\n",
" ins.rz(q, np.pi / 4)"
]
},
{
Expand Down Expand Up @@ -343,7 +344,7 @@
],
"source": [
"success_count = len([x for x in result.measurements[\"c\"] if x == \"0000\"])\n",
"print(\"success ratio: \", success_count/n_shots)"
"print(\"success ratio: \", success_count / n_shots)"
]
},
{
Expand All @@ -366,8 +367,8 @@
"source": [
"@aq.subroutine\n",
"def basis_rotation_t_type(q: int):\n",
" ins.rz(q, -np.pi/4)\n",
" ins.ry(q, -np.arccos(1/np.sqrt(3)))"
" ins.rz(q, -np.pi / 4)\n",
" ins.ry(q, -np.arccos(1 / np.sqrt(3)))"
]
},
{
Expand Down Expand Up @@ -436,7 +437,7 @@
"source": [
"result = LocalSimulator().run(distillation_rus, shots=20).result()\n",
"counts = Counter(result.measurements[\"c2\"])\n",
"probs = {str(k):v/sum(counts.values()) for k,v in counts.items()}\n",
"probs = {str(k): v / sum(counts.values()) for k, v in counts.items()}\n",
"\n",
"expval = probs.get(\"0\", 0) - probs.get(\"1\", 0)\n",
"print(\"Z expectation value: \", expval)"
Expand Down
1 change: 1 addition & 0 deletions examples/4_Native_programming.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@
" cnot(\"$0\", \"$1\")\n",
" return measure([\"$0\", \"$1\"])\n",
"\n",
"\n",
"try:\n",
" bell_state.build(device=Devices.IonQ.Aria1)\n",
"except Exception as e:\n",
Expand Down
16 changes: 10 additions & 6 deletions examples/5_Pulse_programming_and_dynamical_decoupling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
"qubit = 0\n",
"idle_duration = 10e-6\n",
"\n",
"\n",
"@aq.main\n",
"def idle():\n",
" control_frame = device.frames[f\"q{qubit}_rf_frame\"]\n",
Expand Down Expand Up @@ -162,11 +163,13 @@
"source": [
"pi = np.pi\n",
"\n",
"\n",
"def x_pulse(qubit: int):\n",
" # Pi pulse apply on X-axis of Bloch sphere\n",
" qubit = f\"${qubit}\"\n",
" rx(qubit, pi)\n",
"\n",
"\n",
"def y_pulse(qubit: int):\n",
" # Pi pulse apply on Y-axis of Bloch sphere\n",
" qubit = f\"${qubit}\"\n",
Expand Down Expand Up @@ -198,22 +201,23 @@
"idle_duration = 10e-6\n",
"n_cycles = 3\n",
"\n",
"\n",
"@aq.main\n",
"def idle_with_dd():\n",
" dd_spacing = idle_duration / (4*n_cycles)\n",
" dd_spacing = idle_duration / (4 * n_cycles)\n",
"\n",
" control_frame = device.frames[f\"q{qubit}_rf_frame\"]\n",
" \n",
"\n",
" pulse.delay(control_frame, dd_spacing)\n",
" for _ in aq.range(n_cycles):\n",
" x_pulse(qubit)\n",
" pulse.delay(control_frame, 2*dd_spacing)\n",
" pulse.delay(control_frame, 2 * dd_spacing)\n",
" y_pulse(qubit)\n",
" pulse.delay(control_frame, 2*dd_spacing)\n",
" pulse.delay(control_frame, 2 * dd_spacing)\n",
" x_pulse(qubit)\n",
" pulse.delay(control_frame, 2*dd_spacing)\n",
" pulse.delay(control_frame, 2 * dd_spacing)\n",
" y_pulse(qubit)\n",
" \n",
"\n",
" pulse.delay(control_frame, dd_spacing)"
]
},
Expand Down
13 changes: 8 additions & 5 deletions examples/6_Customize_gate_calibrations.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,10 @@
"wf = DragGaussianWaveform(\n",
" 24e-9, 2.547965400864e-9, 2.370235498840002e-10, 0.293350447987059, False, \"wf_drag_gaussian_1\"\n",
")\n",
"q0_rf_frame = device.frames['q0_rf_frame']\n",
"q0_rf_frame = device.frames[\"q0_rf_frame\"]\n",
"\n",
"@aq.gate_calibration(implements=rx, target=\"$0\", angle=np.pi/2)\n",
"\n",
"@aq.gate_calibration(implements=rx, target=\"$0\", angle=np.pi / 2)\n",
"def my_rx_cal():\n",
" pulse.barrier(\"$0\")\n",
" pulse.shift_frequency(q0_rf_frame, -321047.14178613486 - 100)\n",
Expand Down Expand Up @@ -242,10 +243,11 @@
"source": [
"@aq.main\n",
"def my_program():\n",
" rx(\"$0\", np.pi/2)\n",
" rx(\"$0\", np.pi / 2)\n",
" rz(\"$1\", 0.123)\n",
" measure(\"$0\")\n",
"\n",
"\n",
"print(my_program.build().to_ir())"
]
},
Expand Down Expand Up @@ -307,12 +309,13 @@
"metadata": {},
"outputs": [],
"source": [
"q1_rf_frame = device.frames['q1_rf_frame']\n",
"q1_rf_frame = device.frames[\"q1_rf_frame\"]\n",
"\n",
"\n",
"@aq.gate_calibration(implements=rz, target=\"$1\")\n",
"def my_rz_cal(angle: float):\n",
" pulse.barrier(\"$1\")\n",
" pulse.shift_frequency(q1_rf_frame, -1.0*angle)\n",
" pulse.shift_frequency(q1_rf_frame, -1.0 * angle)\n",
" pulse.barrier(\"$1\")"
]
},
Expand Down

0 comments on commit 7e2a770

Please sign in to comment.