From 636725b8cb5c077e221fa8537f569bf2928f0230 Mon Sep 17 00:00:00 2001
From: Megan <42215983+mlk621@users.noreply.github.com>
Date: Wed, 20 Dec 2023 14:43:21 -0600
Subject: [PATCH] update mcmr example date
---
examples/Quantinuum_mid_circuit_measurement.ipynb | 2 +-
examples/python/Quantinuum_mid_circuit_measurement.py | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/examples/Quantinuum_mid_circuit_measurement.ipynb b/examples/Quantinuum_mid_circuit_measurement.ipynb
index b4ecfb6e..7683556b 100644
--- a/examples/Quantinuum_mid_circuit_measurement.ipynb
+++ b/examples/Quantinuum_mid_circuit_measurement.ipynb
@@ -1 +1 @@
-{"cells": [{"cell_type": "markdown", "metadata": {}, "source": ["
\n", "
"]}, {"cell_type": "markdown", "metadata": {}, "source": ["# Mid-Circuit Measurement"]}, {"cell_type": "markdown", "metadata": {}, "source": ["This notebook performs a repetition code calculation with the following H-Series features:"]}, {"cell_type": "markdown", "metadata": {}, "source": ["* Qubit Reuse via Mid-circuit measurements with reset
\n", "* Classicaly-conditioned operations"]}, {"cell_type": "markdown", "metadata": {}, "source": ["The notebook performs a D=3, T=1 repetition code. Three physical qubits are used to encode one logical qubit. The physical qubit register is initialised in the $|000\\rangle$ state encoding the logical $|0\\rangle$ state."]}, {"cell_type": "markdown", "metadata": {}, "source": ["One ancilla qubit is used to perform two syndrome measurements:"]}, {"cell_type": "markdown", "metadata": {}, "source": ["1. $\\hat{Z}_{q[0]} \\hat{Z}_{q[1]} \\hat{I}_{q[2]}$
\n", "2. $\\hat{I}_{q[0]} \\hat{Z}_{q[1]} \\hat{Z}_{q[2]}$"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Subsequently, classically-conditioned operations are used to correct any errors on the physical qubits using the syndrome measurement results. Finally, direct measurements on the physical qubits are performed to verify the final state of the logical qubit is $|0\\rangle$."]}, {"cell_type": "markdown", "metadata": {}, "source": ["## Syndrome Measurement Circuit Primitive"]}, {"cell_type": "markdown", "metadata": {}, "source": ["In the code cell below, a circuit primitive is defined to detect errors on two physical qubits with one ancilla qubit."]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["from pytket.circuit import Circuit, OpType, CircBox\n", "from pytket.circuit.display import render_circuit_jupyter"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["def syndrome_extraction():\n", " circuit = Circuit(3, 1)\n", " circuit.CX(1, 0)\n", " circuit.CX(2, 0)\n", " circuit.Measure(0, 0)\n", " circuit.add_gate(OpType.Reset, [0])\n", " return CircBox(circuit)"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["syndrome_box = syndrome_extraction()\n", "render_circuit_jupyter(syndrome_box.get_circuit())"]}, {"cell_type": "markdown", "metadata": {}, "source": ["## Repetition Code Circuit"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Initially, a `pytket.circuit.Circuit` is instantiated with three physical qubits (`data` register), one ancilla qubits (`ancilla` register). Additionally, two classical registers are added: the first to store output from syndrome measurements (`syndrome` register); and the second (`output` register) to store output from direct measurement on phyiscal qubits."]}, {"cell_type": "markdown", "metadata": {}, "source": ["The use of mid-circuit measurement is straightforward. Note the use of `measure` and `reset` on the ancilla qubits. This example also utilizes conditional logic available with Quantinuum devices as well as Registers and IDs available in `pytket`. See [Classical and conditional operations](https://tket.quantinuum.com/user-manual/manual_circuit.html#classical-and-conditional-operations) and [Registers and IDs](https://tket.quantinuum.com/user-manual/manual_circuit.html#registers-and-ids) for additional examples."]}, {"cell_type": "markdown", "metadata": {}, "source": ["The circuit is named \"Repetition Code\". This name is used by the Job submitted to H-series later in this notebook."]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["from pytket.circuit import Circuit"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Set up circuit object"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["circuit = Circuit(name=\"Repetition Code\")"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Reserve registries"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Add qubit register, the data qubits"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["data = circuit.add_q_register(\"data\", 3)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Add qubit register, the ancilla qubit"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["ancilla = circuit.add_q_register(\"ancilla\", 1)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Add classical registers for the syndromes"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["syndrome = circuit.add_c_register(\"syndrome\", 2)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Add classical registers for the output"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["output = circuit.add_c_register(\"output\", 3)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["The syndrome measurement primitive, defined above, is added twice as `pytket.circuit.CircBox`. The first measures $\\hat{Z}_{q[0]} \\hat{Z}_{q[1]} \\hat{I}_{q[2]}$ and the second measures $\\hat{I}_{q[0]} \\hat{Z}_{q[1]} \\hat{Z}_{q[2]}$. This is one round of syndrome measurements. The `CircBox` instances are decomposed with `pytket.passes.DecomposeBoxes`."]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["from pytket.passes import DecomposeBoxes"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Syndrome Extraction 1: ZZI"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["circuit.add_circbox(syndrome_box, [ancilla[0], data[0], data[1], syndrome[0]])"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Syndrome Extraction 2: IZZ"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["circuit.add_circbox(syndrome_box, [ancilla[0], data[1], data[2], syndrome[1]])\n", "DecomposeBoxes().apply(circuit)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["In the cell below, classically-conditioned operations (`pytket.circuit.OpType.X`) are performed using `pytket.circuit.logic_exp.reg_eq`. The function, `reg_eq`, checks if the measurement output stored in the classical register is equivalent to a particular value. If the equiavlence check is `True`, the desired operation is applied to the specified qubit."]}, {"cell_type": "markdown", "metadata": {}, "source": ["The `X` operation is applied to qubit `data[0]`. The reg_ex checks if the classical output is 01 (little endian - syndrome[0] = 1 and syndrome[1] = 0)."]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["from pytket.circuit.logic_exp import reg_eq"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["circuit.X(data[0], condition=reg_eq(syndrome, 1))"]}, {"cell_type": "markdown", "metadata": {}, "source": ["The `X` operation is applied to qubit `data[2]`. The reg_ex checks if the classical output is 10 (syndrome[0] = 0 and syndrome[1] = 1). If there is no error from the first syndrome measurement (syndrome[0] = 0), but error from the second syndrome measurement (syndrome[1] = 1), then there is a bitflip on the qubit `data[2]`."]}, {"cell_type": "markdown", "metadata": {}, "source": ["if(syndromes==2) -> 01 -> check 1 bad -> X on qubit 2"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["circuit.X(data[2], condition=reg_eq(syndrome, 2))"]}, {"cell_type": "markdown", "metadata": {}, "source": ["The `X` operation is applied to qubit `data[1]`. The reg_ex checks if the classical output is 11 (syndrome[0] = 1 and syndrome[1] = 1). If there is error from the first syndrome measurement (syndrome[0] = 1) and error from the second syndrome measurement (syndrome[1] = 1), then there is a bitflip on the qubit `data[1]`."]}, {"cell_type": "markdown", "metadata": {}, "source": ["# if(syndromes==3) -> 11 -> check 1 and 2 bad -> X on qubit 1"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["circuit.X(data[1], condition=reg_eq(syndrome, 3))"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Finally, measurement gates are added to the `data` qubit register."]}, {"cell_type": "markdown", "metadata": {}, "source": ["Measure out data qubits"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["circuit.Measure(data[0], output[0])\n", "circuit.Measure(data[1], output[1])\n", "circuit.Measure(data[2], output[2])"]}, {"cell_type": "markdown", "metadata": {}, "source": ["The display tool in pytket is used to visualise the circuit in jupyter."]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["from pytket.circuit.display import render_circuit_jupyter"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["render_circuit_jupyter(circuit)"]}, {"cell_type": "markdown", "metadata": {}, "source": [" Select Device"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Login to the Quantinuum API using your credentials and check the device status."]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["from pytket.extensions.quantinuum import QuantinuumBackend"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["machine = \"H1-1E\"\n", "backend = QuantinuumBackend(device_name=machine)\n", "backend.login()"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["print(machine, \"status:\", backend.device_state(device_name=machine))"]}, {"cell_type": "markdown", "metadata": {}, "source": ["### Circuit Compilation"]}, {"cell_type": "markdown", "metadata": {}, "source": ["`pytket` includes many features for optimizing circuits. This includes reducing the number of gates where possible and resynthesizing circuits for a quantum computer's native gate set. See the `pytket` [User Manual](https://tket.quantinuum.com/user-manual/) for more information on all the options that are available."]}, {"cell_type": "markdown", "metadata": {}, "source": ["Here the circuit is compiled with `get_compiled_circuit`, which includes optimizing the gates and resynthesizing the circuit to Quantinuum's native gate set. The `optimisation_level` sets the level of optimisation to perform during compilation, check [Default Compilation](https://cqcl.github.io/pytket-quantinuum/api/index.html#default-compilation) in the pytket-quantinuum documentation for more details."]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["compiled_circuit = backend.get_compiled_circuit(circuit, optimisation_level=2)"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["render_circuit_jupyter(compiled_circuit)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["## Submit and Run the Circuit"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["n_shots = 100\n", "h1_cost = backend.cost(compiled_circuit, n_shots=n_shots, syntax_checker=\"H1-1SC\")\n", "print(f\"Cost: {h1_cost} HQC\")"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["handle = backend.process_circuit(compiled_circuit, n_shots=n_shots)\n", "print(handle)"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["status = backend.circuit_status(handle)\n", "print(status)"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["import json"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["result = backend.get_result(handle)"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["with open(\"pytket_mcmr_example.json\", \"w\") as file:\n", " json.dump(result.to_dict(), file)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["## Analyze Results"]}, {"cell_type": "markdown", "metadata": {}, "source": ["We will now take the raw results and apply a majority vote to determine how many times we got 0 vs 1."]}, {"cell_type": "markdown", "metadata": {}, "source": ["First, define a majority vote function."]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["def majority(result):\n", " \"\"\"Returns whether the output should be considered a 0 or 1.\"\"\"\n", " if result.count(0) > result.count(1):\n", " return 0\n", " elif result.count(0) < result.count(1):\n", " return 1\n", " else:\n", " raise Exception(\"count(0) should not equal count(1)\")"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Now process the output:"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["result_output_cnts = result.get_counts([output[i] for i in range(output.size)])"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["result_output_cnts"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Here, determine how many times 0 vs 1 was observed using the majority vote function."]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["zeros = 0 # Counts the shots with majority zeros\n", "ones = 0 # Counts the shots with majority ones"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["for out in result_output_cnts:\n", " m = majority(out)\n", " if m == 0:\n", " zeros += result_output_cnts[out]\n", " else:\n", " ones += result_output_cnts[out]"]}, {"cell_type": "markdown", "metadata": {}, "source": ["A logical zero was initialized, so our error rate should be number of ones / total number of shots: `ones/shots`"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["p = ones / n_shots\n", "print(f\"The error-rate is: p = {p}\")"]}, {"cell_type": "markdown", "metadata": {}, "source": [" © 2023 by Quantinuum. All Rights Reserved.
"]}], "metadata": {"kernelspec": {"display_name": "Python 3", "language": "python", "name": "python3"}, "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.6.4"}}, "nbformat": 4, "nbformat_minor": 2}
\ No newline at end of file
+{"cells": [{"cell_type": "markdown", "metadata": {}, "source": ["\n", "
"]}, {"cell_type": "markdown", "metadata": {}, "source": ["# Mid-Circuit Measurement"]}, {"cell_type": "markdown", "metadata": {}, "source": ["This notebook performs a repetition code calculation with the following H-Series features:"]}, {"cell_type": "markdown", "metadata": {}, "source": ["* Qubit Reuse via Mid-circuit measurements with reset
\n", "* Classicaly-conditioned operations"]}, {"cell_type": "markdown", "metadata": {}, "source": ["The notebook performs a D=3, T=1 repetition code. Three physical qubits are used to encode one logical qubit. The physical qubit register is initialised in the $|000\\rangle$ state encoding the logical $|0\\rangle$ state."]}, {"cell_type": "markdown", "metadata": {}, "source": ["One ancilla qubit is used to perform two syndrome measurements:"]}, {"cell_type": "markdown", "metadata": {}, "source": ["1. $\\hat{Z}_{q[0]} \\hat{Z}_{q[1]} \\hat{I}_{q[2]}$
\n", "2. $\\hat{I}_{q[0]} \\hat{Z}_{q[1]} \\hat{Z}_{q[2]}$"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Subsequently, classically-conditioned operations are used to correct any errors on the physical qubits using the syndrome measurement results. Finally, direct measurements on the physical qubits are performed to verify the final state of the logical qubit is $|0\\rangle$."]}, {"cell_type": "markdown", "metadata": {}, "source": ["## Syndrome Measurement Circuit Primitive"]}, {"cell_type": "markdown", "metadata": {}, "source": ["In the code cell below, a circuit primitive is defined to detect errors on two physical qubits with one ancilla qubit."]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["from pytket.circuit import Circuit, OpType, CircBox\n", "from pytket.circuit.display import render_circuit_jupyter"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["def syndrome_extraction():\n", " circuit = Circuit(3, 1)\n", " circuit.CX(1, 0)\n", " circuit.CX(2, 0)\n", " circuit.Measure(0, 0)\n", " circuit.add_gate(OpType.Reset, [0])\n", " return CircBox(circuit)"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["syndrome_box = syndrome_extraction()\n", "render_circuit_jupyter(syndrome_box.get_circuit())"]}, {"cell_type": "markdown", "metadata": {}, "source": ["## Repetition Code Circuit"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Initially, a `pytket.circuit.Circuit` is instantiated with three physical qubits (`data` register), one ancilla qubits (`ancilla` register). Additionally, two classical registers are added: the first to store output from syndrome measurements (`syndrome` register); and the second (`output` register) to store output from direct measurement on phyiscal qubits."]}, {"cell_type": "markdown", "metadata": {}, "source": ["The use of mid-circuit measurement is straightforward. Note the use of `measure` and `reset` on the ancilla qubits. This example also utilizes conditional logic available with Quantinuum devices as well as Registers and IDs available in `pytket`. See [Classical and conditional operations](https://tket.quantinuum.com/user-manual/manual_circuit.html#classical-and-conditional-operations) and [Registers and IDs](https://tket.quantinuum.com/user-manual/manual_circuit.html#registers-and-ids) for additional examples."]}, {"cell_type": "markdown", "metadata": {}, "source": ["The circuit is named \"Repetition Code\". This name is used by the Job submitted to H-series later in this notebook."]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["from pytket.circuit import Circuit"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Set up circuit object"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["circuit = Circuit(name=\"Repetition Code\")"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Reserve registries"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Add qubit register, the data qubits"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["data = circuit.add_q_register(\"data\", 3)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Add qubit register, the ancilla qubit"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["ancilla = circuit.add_q_register(\"ancilla\", 1)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Add classical registers for the syndromes"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["syndrome = circuit.add_c_register(\"syndrome\", 2)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Add classical registers for the output"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["output = circuit.add_c_register(\"output\", 3)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["The syndrome measurement primitive, defined above, is added twice as `pytket.circuit.CircBox`. The first measures $\\hat{Z}_{q[0]} \\hat{Z}_{q[1]} \\hat{I}_{q[2]}$ and the second measures $\\hat{I}_{q[0]} \\hat{Z}_{q[1]} \\hat{Z}_{q[2]}$. This is one round of syndrome measurements. The `CircBox` instances are decomposed with `pytket.passes.DecomposeBoxes`."]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["from pytket.passes import DecomposeBoxes"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Syndrome Extraction 1: ZZI"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["circuit.add_circbox(syndrome_box, [ancilla[0], data[0], data[1], syndrome[0]])"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Syndrome Extraction 2: IZZ"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["circuit.add_circbox(syndrome_box, [ancilla[0], data[1], data[2], syndrome[1]])\n", "DecomposeBoxes().apply(circuit)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["In the cell below, classically-conditioned operations (`pytket.circuit.OpType.X`) are performed using `pytket.circuit.logic_exp.reg_eq`. The function, `reg_eq`, checks if the measurement output stored in the classical register is equivalent to a particular value. If the equiavlence check is `True`, the desired operation is applied to the specified qubit."]}, {"cell_type": "markdown", "metadata": {}, "source": ["The `X` operation is applied to qubit `data[0]`. The reg_ex checks if the classical output is 01 (little endian - syndrome[0] = 1 and syndrome[1] = 0)."]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["from pytket.circuit.logic_exp import reg_eq"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["circuit.X(data[0], condition=reg_eq(syndrome, 1))"]}, {"cell_type": "markdown", "metadata": {}, "source": ["The `X` operation is applied to qubit `data[2]`. The reg_ex checks if the classical output is 10 (syndrome[0] = 0 and syndrome[1] = 1). If there is no error from the first syndrome measurement (syndrome[0] = 0), but error from the second syndrome measurement (syndrome[1] = 1), then there is a bitflip on the qubit `data[2]`."]}, {"cell_type": "markdown", "metadata": {}, "source": ["if(syndromes==2) -> 01 -> check 1 bad -> X on qubit 2"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["circuit.X(data[2], condition=reg_eq(syndrome, 2))"]}, {"cell_type": "markdown", "metadata": {}, "source": ["The `X` operation is applied to qubit `data[1]`. The reg_ex checks if the classical output is 11 (syndrome[0] = 1 and syndrome[1] = 1). If there is error from the first syndrome measurement (syndrome[0] = 1) and error from the second syndrome measurement (syndrome[1] = 1), then there is a bitflip on the qubit `data[1]`."]}, {"cell_type": "markdown", "metadata": {}, "source": ["# if(syndromes==3) -> 11 -> check 1 and 2 bad -> X on qubit 1"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["circuit.X(data[1], condition=reg_eq(syndrome, 3))"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Finally, measurement gates are added to the `data` qubit register."]}, {"cell_type": "markdown", "metadata": {}, "source": ["Measure out data qubits"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["circuit.Measure(data[0], output[0])\n", "circuit.Measure(data[1], output[1])\n", "circuit.Measure(data[2], output[2])"]}, {"cell_type": "markdown", "metadata": {}, "source": ["The display tool in pytket is used to visualise the circuit in jupyter."]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["from pytket.circuit.display import render_circuit_jupyter"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["render_circuit_jupyter(circuit)"]}, {"cell_type": "markdown", "metadata": {}, "source": [" Select Device"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Login to the Quantinuum API using your credentials and check the device status."]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["from pytket.extensions.quantinuum import QuantinuumBackend"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["machine = \"H1-1E\"\n", "backend = QuantinuumBackend(device_name=machine)\n", "backend.login()"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["print(machine, \"status:\", backend.device_state(device_name=machine))"]}, {"cell_type": "markdown", "metadata": {}, "source": ["### Circuit Compilation"]}, {"cell_type": "markdown", "metadata": {}, "source": ["`pytket` includes many features for optimizing circuits. This includes reducing the number of gates where possible and resynthesizing circuits for a quantum computer's native gate set. See the `pytket` [User Manual](https://tket.quantinuum.com/user-manual/) for more information on all the options that are available."]}, {"cell_type": "markdown", "metadata": {}, "source": ["Here the circuit is compiled with `get_compiled_circuit`, which includes optimizing the gates and resynthesizing the circuit to Quantinuum's native gate set. The `optimisation_level` sets the level of optimisation to perform during compilation, check [Default Compilation](https://cqcl.github.io/pytket-quantinuum/api/index.html#default-compilation) in the pytket-quantinuum documentation for more details."]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["compiled_circuit = backend.get_compiled_circuit(circuit, optimisation_level=2)"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["render_circuit_jupyter(compiled_circuit)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["## Submit and Run the Circuit"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["n_shots = 100\n", "h1_cost = backend.cost(compiled_circuit, n_shots=n_shots, syntax_checker=\"H1-1SC\")\n", "print(f\"Cost: {h1_cost} HQC\")"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["handle = backend.process_circuit(compiled_circuit, n_shots=n_shots)\n", "print(handle)"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["status = backend.circuit_status(handle)\n", "print(status)"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["import json"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["result = backend.get_result(handle)"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["with open(\"pytket_mcmr_example.json\", \"w\") as file:\n", " json.dump(result.to_dict(), file)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["## Analyze Results"]}, {"cell_type": "markdown", "metadata": {}, "source": ["We will now take the raw results and apply a majority vote to determine how many times we got 0 vs 1."]}, {"cell_type": "markdown", "metadata": {}, "source": ["First, define a majority vote function."]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["def majority(result):\n", " \"\"\"Returns whether the output should be considered a 0 or 1.\"\"\"\n", " if result.count(0) > result.count(1):\n", " return 0\n", " elif result.count(0) < result.count(1):\n", " return 1\n", " else:\n", " raise Exception(\"count(0) should not equal count(1)\")"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Now process the output:"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["result_output_cnts = result.get_counts([output[i] for i in range(output.size)])"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["result_output_cnts"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Here, determine how many times 0 vs 1 was observed using the majority vote function."]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["zeros = 0 # Counts the shots with majority zeros\n", "ones = 0 # Counts the shots with majority ones"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["for out in result_output_cnts:\n", " m = majority(out)\n", " if m == 0:\n", " zeros += result_output_cnts[out]\n", " else:\n", " ones += result_output_cnts[out]"]}, {"cell_type": "markdown", "metadata": {}, "source": ["A logical zero was initialized, so our error rate should be number of ones / total number of shots: `ones/shots`"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["p = ones / n_shots\n", "print(f\"The error-rate is: p = {p}\")"]}, {"cell_type": "markdown", "metadata": {}, "source": [" © 2024 by Quantinuum. All Rights Reserved.
"]}], "metadata": {"kernelspec": {"display_name": "Python 3", "language": "python", "name": "python3"}, "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.6.4"}}, "nbformat": 4, "nbformat_minor": 2}
\ No newline at end of file
diff --git a/examples/python/Quantinuum_mid_circuit_measurement.py b/examples/python/Quantinuum_mid_circuit_measurement.py
index 64eb3750..b07dd576 100644
--- a/examples/python/Quantinuum_mid_circuit_measurement.py
+++ b/examples/python/Quantinuum_mid_circuit_measurement.py
@@ -188,4 +188,4 @@ def majority(result):
p = ones / n_shots
print(f"The error-rate is: p = {p}")
-# © 2023 by Quantinuum. All Rights Reserved.
+# © 2024 by Quantinuum. All Rights Reserved.