{"cells":[{"cell_type":"markdown","metadata":{},"source":["# Conditional gates\n","\n","**Download this notebook - {nb-download}`conditional_gate_example.ipynb`**"]},{"cell_type":"markdown","metadata":{},"source":["Whilst any quantum process can be created by performing \"pure\" operations delaying all measurements to the end, this is not always practical and can greatly increase the resource requirements. It is much more convenient to alternate quantum gates and measurements, especially if we can use the measurement results to determine which gates to apply (we refer to this more generic circuit model as \"mixed\" circuits, against the usual \"pure\" circuits). This is especially crucial for error correcting codes, where the correction gates are applied only if an error is detected.
\n","
\n","Measurements on many NISQ devices are often slow and it is hard to maintain other qubits in a quantum state during the measurement operation. Hence they may only support a single round of measurements at the end of the circuit, removing the need for conditional gate support. However, the ability to work with mid-circuit measurement and conditional gates is a feature in high demand for the future, and tket is ready for it.
\n","
\n","Not every circuit language specification supports conditional gates in the same way. The most popular circuit model at the moment is that provided by the OpenQASM language. This permits a very restricted model of classical logic, where we can apply a gate conditionally on the exact value of a classical register. There is no facility in the current spec for Boolean logic or classical operations to apply any function to the value prior to the equality check.
\n","
\n","For example, quantum teleportation can be performed by the following QASM:
\n","`OPENQASM 2.0;`
\n","`include \"qelib1.inc\";`
\n","`qreg a[2];`
\n","`qreg b[1];`
\n","`creg c[2];`
\n","`// Bell state between Alice and Bob`
\n","`h a[1];`
\n","`cx a[1],b[0];`
\n","`// Bell measurement of Alice's qubits`
\n","`cx a[0],a[1];`
\n","`h a[0];`
\n","`measure a[0] -> c[0];`
\n","`measure a[1] -> c[1];`
\n","`// Correction of Bob's qubit`
\n","`if(c==1) z b[0];`
\n","`if(c==3) z b[0];`
\n","`if(c==2) x b[0];`
\n","`if(c==3) x b[0];`"]},{"cell_type":"markdown","metadata":{},"source":["tket supports a slightly more general form of conditional gates, where the gate is applied conditionally on the exact value of any list of bits. When adding a gate to a `Circuit` object, pass in the kwargs `condition_bits` and `condition_value` and the gate will only be applied if the state of the bits yields the binary representation of the value."]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["from pytket import Circuit\n","from pytket.circuit.display import render_circuit_jupyter"]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["circ = Circuit()\n","alice = circ.add_q_register(\"a\", 2)\n","bob = circ.add_q_register(\"b\", 1)\n","cr = circ.add_c_register(\"c\", 2)\n","# Bell state between Alice and Bob:\n","circ.H(alice[1])\n","circ.CX(alice[1], bob[0])\n","# Bell measurement of Alice's qubits:\n","circ.CX(alice[0], alice[1])\n","circ.H(alice[0])\n","circ.Measure(alice[0], cr[0])\n","circ.Measure(alice[1], cr[1])\n","# Correction of Bob's qubit:\n","circ.Z(bob[0], condition_bits=[cr[0]], condition_value=1)\n","circ.X(bob[0], condition_bits=[cr[1]], condition_value=1)\n","render_circuit_jupyter(circ)"]},{"cell_type":"markdown","metadata":{},"source":["Performing individual gates conditionally is sufficient, but can get cumbersome for larger circuits. Fortunately, tket's Box structures can also be performed conditionally, enabling this to be applied to large circuits with ease.
\n","
\n","For the sake of example, assume our device struggles to perform $X$ gates. We can surround it by $CX$ gates onto an ancilla, so measuring the ancilla will either result in the identity or $X$ being applied to the target qubit. If we detect that the $X$ fails, we can retry."]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["from pytket.circuit import CircBox, Qubit, Bit"]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["checked_x = Circuit(2, 1)\n","checked_x.CX(0, 1)\n","checked_x.X(0)\n","checked_x.CX(0, 1)\n","checked_x.Measure(1, 0)\n","x_box = CircBox(checked_x)"]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["circ2 = Circuit()\n","target = Qubit(\"t\", 0)\n","ancilla = Qubit(\"a\", 0)\n","success = Bit(\"s\", 0)\n","circ2.add_qubit(target)\n","circ2.add_qubit(ancilla)\n","circ2.add_bit(success)"]},{"cell_type":"markdown","metadata":{},"source":["Try the X gate:"]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["circ2.add_gate(x_box, args=[target, ancilla, success])\n","# Try again if the X failed\n","circ2.add_gate(\n"," x_box, args=[target, ancilla, success], condition_bits=[success], condition_value=0\n",")\n","render_circuit_jupyter(circ2)"]},{"cell_type":"markdown","metadata":{},"source":["tket is able to apply essential compilation passes on circuits containing conditional gates. This includes decomposing any boxes into primitive gates and rebasing to other gatesets whilst preserving the conditional data."]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["from pytket.passes import DecomposeBoxes, RebaseTket, SequencePass"]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["comp_pass = SequencePass([DecomposeBoxes(), RebaseTket()])\n","comp_pass.apply(circ2)\n","render_circuit_jupyter(circ2)"]},{"cell_type":"markdown","metadata":{},"source":["A tket circuit can be converted to OpenQASM or other languages following the same classical model (e.g. Qiskit) when all conditional gates are dependent on the exact state of a single, whole classical register."]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["from pytket.extensions.qiskit import tk_to_qiskit"]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["qc = tk_to_qiskit(circ2)\n","print(qc)"]},{"cell_type":"markdown","metadata":{},"source":["This allows us to test our mixed programs using the `AerBackend`."]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["from pytket.extensions.qiskit import AerBackend"]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["circ3 = Circuit(2, 1)\n","circ3.Rx(0.3, 0)\n","circ3.Measure(0, 0)\n","# Set qubit 1 to be the opposite result and measure\n","circ3.X(1, condition_bits=[0], condition_value=0)\n","circ3.Measure(1, 0)\n","backend = AerBackend()\n","compiled_circ = backend.get_compiled_circuit(circ3)\n","render_circuit_jupyter(compiled_circ)"]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["counts = backend.run_circuit(compiled_circ, 1024).get_counts()\n","print(counts)"]},{"cell_type":"markdown","metadata":{},"source":["Try out mid-circuit measurement and conditional gate support on the `AerBackend` simulator, or ask about accessing the `QuantinuumBackend` to try on a hardware device."]}],"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}