{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Circuit analysis\n",
"\n",
"**Download this notebook - {nb-download}`circuit_analysis_example.ipynb`**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This notebook will introduce the basic methods of analysis and visualization of circuits available in `pytket`.
\n",
"
\n",
"It makes use of the modules `pytket_qiskit` and `pytket_cirq` for visualization; these need to be installed (with `pip`) in addition to `pytket`.
\n",
"
\n",
"We'll start by generating a small circuit to use as an example, and give it a name."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from pytket.circuit import Circuit, OpType"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[CU1(0.5) q[0], q[1]; Y q[2]; Z q[3]; H q[0]; X q[1]; H q[3]; X q[0]; CX q[1], q[2]; Y q[0]; Y q[1]; Z q[2]; Z q[0]; Z q[1]; CU1(0.5) q[2], q[3]; H q[1]; H q[2]; X q[3]; X q[2]; Y q[3]; CX q[3], q[0]; ]"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"c = Circuit(4, name=\"example\")\n",
"c.add_gate(OpType.CU1, 0.5, [0, 1])\n",
"c.H(0).X(1).Y(2).Z(3)\n",
"c.X(0).CX(1, 2).Y(1).Z(2).H(3)\n",
"c.Y(0).Z(1)\n",
"c.add_gate(OpType.CU1, 0.5, [2, 3])\n",
"c.H(2).X(3)\n",
"c.Z(0).H(1).X(2).Y(3).CX(3, 0)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Basic statistics"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"From the circuit we can easily read off the number of qubits ..."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"c.n_qubits"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"... the name ..."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'example'"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"c.name"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"... the overall depth of the circuit ..."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"8"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"c.depth()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"... and the depth by type of gate:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"from pytket.circuit import OpType"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(2, 2)"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"c.depth_by_type(OpType.CU1), c.depth_by_type(OpType.H)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This last method counts the number of instances of the specified gate type that cannot be parallelized. Notice that although there are 4 H gates in the circuit, the H-depth is 2 because pairs of them can be parallelized (as will be clear from the visualizations below)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Visualization"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"There are several ways to produce useful visualizations of circuits from `pytket`: we can use the methods in the `pytket.circuit.display` class; we can use the built-in `Graph` class to visualize the circuit as a directed acyclic graph (DAG); we can convert the circuit to either Qiskit or Cirq and use the tools provided by those modules; or we can export to Latex."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Via the `render_circuit_jupyter` method"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"from pytket.circuit.display import render_circuit_jupyter"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"