-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
2,360 additions
and
7 deletions.
There are no files selected for viewing
683 changes: 682 additions & 1 deletion
683
docs/examples/algorithms_and_protocols/phase_estimation.ipynb
Large diffs are not rendered by default.
Oops, something went wrong.
386 changes: 385 additions & 1 deletion
386
docs/examples/algorithms_and_protocols/pytket-qujax_qaoa.ipynb
Large diffs are not rendered by default.
Oops, something went wrong.
212 changes: 211 additions & 1 deletion
212
docs/examples/circuit_compilation/contextual_optimisation.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,211 @@ | ||
{"cells": [{"cell_type": "markdown", "metadata": {}, "source": ["# Contextual optimisation"]}, {"cell_type": "markdown", "metadata": {}, "source": ["This notebook will illustrate the techniques of \"contextual optimisation\" available in TKET."]}, {"cell_type": "markdown", "metadata": {}, "source": ["See the user manaul for an introduction to the concept and methods. Here we will present an example showing how we can save some gates at the beginnning and end of a circuit, making no assumptions about the structure of the circuit."]}, {"cell_type": "markdown", "metadata": {}, "source": ["We will take as an example an ansatz circuit consisting of alternating layers of Ry and CX gates, where some proportion of the Ry angles are zero. This is a typical ansatz for variational algorithms, used for solving diagonal Hamiltonians for combinatorial optimisation."]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["from pytket.circuit import Circuit\n", "from random import random, randrange, seed"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["def random_sparse_ansatz(n_qubits, n_layers, p, rng_seed=None):\n", " seed(rng_seed)\n", " circ = Circuit(n_qubits)\n", " for q in range(n_qubits):\n", " if random() < p:\n", " circ.Ry(0.1 * randrange(20), q)\n", " for l in range(n_layers):\n", " for q in range(0, n_qubits - 1, 2):\n", " circ.CX(q, q + 1)\n", " for q in range(2 * (n_qubits // 2)):\n", " if random() < p:\n", " circ.Ry(0.1 * randrange(20), q)\n", " for q in range(1, n_qubits - 1, 2):\n", " circ.CX(q, q + 1)\n", " for q in range(2 * ((n_qubits - 1) // 2)):\n", " if random() < p:\n", " circ.Ry(0.1 * randrange(20), q + 1)\n", " circ.measure_all()\n", " return circ"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Let's examine a smallish example:"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["from pytket.circuit import OpType\n", "from pytket.circuit.display import render_circuit_jupyter"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["c = random_sparse_ansatz(4, 3, 0.5, rng_seed=0)\n", "render_circuit_jupyter(c)\n", "print(\"Number of CX:\", c.n_gates_of_type(OpType.CX))"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Contextual optimizations allow us to shave some gates from the beginning and end of the circuit. Those at the end get commuted through the Measure gates into a classical post-processing circuit, which we can then pass to `BackendResult` methods to have the postprocessing performed automatically."]}, {"cell_type": "markdown", "metadata": {}, "source": ["The `prepare_circuit()` method returns a pair of circuits, the first of which is what we actually run and the second of specifies the required postprocessing."]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["from pytket.utils import prepare_circuit"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["c0, ppcirc = prepare_circuit(c)\n", "render_circuit_jupyter(c0)\n", "print(\"Number of CX:\", c0.n_gates_of_type(OpType.CX))"]}, {"cell_type": "markdown", "metadata": {}, "source": ["In this case, one CX has been shaved from the beginning of the circuit and two from the end."]}, {"cell_type": "markdown", "metadata": {}, "source": ["We can run the processed circuit on our backend:"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["from pytket.extensions.qiskit import AerBackend"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["b = AerBackend()\n", "c1 = b.get_compiled_circuit(c0)\n", "h = b.process_circuit(c1, n_shots=10)\n", "r = b.get_result(h)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["And finally get the counts or shots, accounting for the classical postprocessing:"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["counts = r.get_counts(ppcirc=ppcirc)\n", "print(counts)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["See the [pytket user manual](https://tket.quantinuum.com/user-guide/manual/manual_compiler.html#contextual-optimisations) for more details about contextual optimisations and how to apply them in TKET."]}], "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} | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Contextual optimisation" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"This notebook will illustrate the techniques of \"contextual optimisation\" available in TKET." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"See the user manaul for an introduction to the concept and methods. Here we will present an example showing how we can save some gates at the beginnning and end of a circuit, making no assumptions about the structure of the circuit." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"We will take as an example an ansatz circuit consisting of alternating layers of Ry and CX gates, where some proportion of the Ry angles are zero. This is a typical ansatz for variational algorithms, used for solving diagonal Hamiltonians for combinatorial optimisation." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from pytket.circuit import Circuit\n", | ||
"from random import random, randrange, seed" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"def random_sparse_ansatz(n_qubits, n_layers, p, rng_seed=None):\n", | ||
" seed(rng_seed)\n", | ||
" circ = Circuit(n_qubits)\n", | ||
" for q in range(n_qubits):\n", | ||
" if random() < p:\n", | ||
" circ.Ry(0.1 * randrange(20), q)\n", | ||
" for l in range(n_layers):\n", | ||
" for q in range(0, n_qubits - 1, 2):\n", | ||
" circ.CX(q, q + 1)\n", | ||
" for q in range(2 * (n_qubits // 2)):\n", | ||
" if random() < p:\n", | ||
" circ.Ry(0.1 * randrange(20), q)\n", | ||
" for q in range(1, n_qubits - 1, 2):\n", | ||
" circ.CX(q, q + 1)\n", | ||
" for q in range(2 * ((n_qubits - 1) // 2)):\n", | ||
" if random() < p:\n", | ||
" circ.Ry(0.1 * randrange(20), q + 1)\n", | ||
" circ.measure_all()\n", | ||
" return circ" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Let's examine a smallish example:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from pytket.circuit import OpType\n", | ||
"from pytket.circuit.display import render_circuit_jupyter" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"c = random_sparse_ansatz(4, 3, 0.5, rng_seed=0)\n", | ||
"render_circuit_jupyter(c)\n", | ||
"print(\"Number of CX:\", c.n_gates_of_type(OpType.CX))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Contextual optimizations allow us to shave some gates from the beginning and end of the circuit. Those at the end get commuted through the Measure gates into a classical post-processing circuit, which we can then pass to `BackendResult` methods to have the postprocessing performed automatically." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"The `prepare_circuit()` method returns a pair of circuits, the first of which is what we actually run and the second of specifies the required postprocessing." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from pytket.utils import prepare_circuit" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"c0, ppcirc = prepare_circuit(c)\n", | ||
"render_circuit_jupyter(c0)\n", | ||
"print(\"Number of CX:\", c0.n_gates_of_type(OpType.CX))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"In this case, one CX has been shaved from the beginning of the circuit and two from the end." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"We can run the processed circuit on our backend:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from pytket.extensions.qiskit import AerBackend" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"b = AerBackend()\n", | ||
"c1 = b.get_compiled_circuit(c0)\n", | ||
"h = b.process_circuit(c1, n_shots=10)\n", | ||
"r = b.get_result(h)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"And finally get the counts or shots, accounting for the classical postprocessing:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"counts = r.get_counts(ppcirc=ppcirc)\n", | ||
"print(counts)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"See the [pytket user manual](../user-guide/manual/manual_compiler.html#contextual-optimisations) for more details about contextual optimisations and how to apply them in TKET." | ||
] | ||
} | ||
], | ||
"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 | ||
} |
1,080 changes: 1,079 additions & 1 deletion
1,080
docs/examples/circuit_construction/circuit_generation_example.ipynb
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters