{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# How to represent quantum systems using graphs\n",
"**Represent quantum systems for optimization, simulation, and other tasks using graphs**\n",
"\n",
"The typical purpose of graph objects in Boulder Opal is to represent quantum systems.\n",
"The graph representation of such a system can be used to perform several tasks, including [robust control](https://docs.q-ctrl.com/boulder-opal/tutorials/design-robust-single-qubit-gates-using-computational-graphs) (for calculating optimized control pulses), [simulation](https://docs.q-ctrl.com/boulder-opal/tutorials/simulate-the-dynamics-of-a-single-qubit-using-computational-graphs) (to understand the dynamics of the system in the presence of specific controls and noises), and [system identification](https://docs.q-ctrl.com/boulder-opal/user-guides/how-to-perform-hamiltonian-parameter-estimation-using-a-small-amount-of-measured-data) (to estimate the values of unknown system parameters based on measurements of the system).\n",
"\n",
"Please refer to our topic [Understanding graphs in Boulder Opal](https://docs.q-ctrl.com/boulder-opal/topics/) for context on what graphs are used for and why.\n",
"\n",
"In what follows, we use graphs to define time-dependent Hamiltonians for simulation or optimization."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Summary workflow\n",
"\n",
"Here we outline the general procedure for representing your quantum systems and the computations performed on them in the form of graph objects.\n",
"\n",
"### 1. Create graph inputs\n",
"For an optimization, these are optimizable variables that can be tuned by the optimizer to minimize a cost function.\n",
"For a simulation, the inputs can be known values for control pulses, or quantities derived from known values.\n",
"\n",
"### 2. Create signals\n",
"*Signals* are scalar-valued functions of time, which may be non-linear, have enforced temporal structure such as time symmetry, or more generally can depend arbitrarily on the inputs.\n",
"They only contain one numerical value in each period of time, which means their shape is zero-dimensional.\n",
"Signals represent the time-dependent envelope of the Hamiltonian.\n",
"You create piecewise-constant (PWC) signals using the `graph.pwc_signal` operation.\n",
"\n",
"### 3. Create Hamiltonian operators\n",
"You create *operators* or PWC operator-valued (2D) functions of time by multiplying constant matrices (for example Pauli matrices) with signals.\n",
"Usually these operators represent individual terms in your Hamiltonian.\n",
"You can also create constant operators to represent static terms in your Hamiltonian.\n",
"Finally, you sum the individual operators created into a single Hamiltonian operator.\n",
"\n",
"### 4. Add graph nodes representing computations on your quantum system\n",
"Once you have defined the nodes that describe your quantum system, you can add extra nodes representing the computations that you want to perform on it.\n",
"For example, Boulder Opal offers [time evolution operations](https://docs.q-ctrl.com/boulder-opal/references/qctrl/Graphs.html#time-evolution), among many others.\n",
"\n",
"Note that while this approach to constructing Hamiltonians is the most common, it is not a requirement.\n",
"You can use graphs to perform a wide variety of other computations too.\n",
"For example, Boulder Opal also provides specialized functions for working with trapped ions systems that take advantage of certain approximations to bypass Hamiltonian-level descriptions of the system (see the [How to design error-robust Mølmer–Sørensen gates for trapped ions](https://docs.q-ctrl.com/boulder-opal/user-guides/how-to-optimize-error-robust-molmer-sorensen-gates-for-trapped-ions) user guide for details)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Worked example: Two-qubit system with a tunable coupling qubit\n",
"\n",
"Consider a system consisting of three transmon qubits, in which the central qubit acts as a tunable coupler between the other two, as proposed by [Yan et al](https://arxiv.org/abs/1803.09813).\n",
"This system can be approximated by the following two-qubit Hamiltonian:\n",
"$$\n",
"\\tilde H = \\frac{\\omega_a}{2} \\sigma_{z, a} +\\frac{\\omega_b}{2} \\sigma_{z, b} + \\frac{g_a^2}{2 \\Delta_a} \\sigma_{z,a}+ \\frac{g_b^2}{2 \\Delta_b} \\sigma_{z,b} + \\tilde g \\left(\\sigma_{+, a} \\sigma_{-,b} + \\sigma_{-,a} \\sigma_{+,b} \\right)\\, ,\n",
"$$\n",
"where $\\omega_k$ are the qubit frequencies, $\\Delta_k=\\omega_k-\\omega_c$ are the detunings of each qubit from the coupling qubit, $g_k$ are the direct couplings between each qubit and the coupling qubit, and $\\tilde g$ is the effective coupling between the two qubits.\n",
"\n",
"The couplings $g_k,\\tilde g$ are given by\n",
"$$ g_k \\approx \\frac{1}{2}\\frac{C_{kc}}{\\sqrt{C_kC_c}}\\sqrt{\\omega_k\\omega_c}, $$\n",
"$$ \\tilde g = \\frac{1}{2}\\left[\\frac{\\omega_c}{2\\Delta}\\eta - \\frac{\\omega_c}{2\\Sigma}\\eta + \\eta + 1\\right]\\frac{C_{ab}}{\\sqrt{C_aC_b}}\\sqrt{\\omega_a\\omega_b}\\, ,$$\n",
"where $C_k,C_c$ are the capacitances of the qubits, $C_{xy}$ are the qubit-qubit capacitances between each pair of qubits, and the derived quantities are given by\n",
"$\\eta=C_{ac}C_{bc}/C_{ab}C_c$, $1/\\Sigma=(1/\\Sigma_a+1/\\Sigma_b)/2$,\n",
"$\\Sigma_k=\\omega_k+\\omega_c$, and\n",
"$1/\\Delta=(1/\\Delta_a+1/\\Delta_b)/2$.\n",
"We can see that the effective coupling strength $\\tilde g$ can be tuned by adjusting the frequency $\\omega_c$ of the coupling qubit.\n",
"\n",
"In this example, we will show how you can represent this system as a graph, and calculate its time evolution operators for a particular choice of the $\\omega_c$ control.\n",
"The chosen controls will produce a gate that is, up to single-qubit phases, close to an iSWAP.\n",
"\n",
"We will consider a simple PWC pulse for $\\omega_c$ which is then passed through imperfect control lines.\n",
"These control lines distort the signal that actually reaches the system, and thus the relevant term in the Hamiltonian.\n",
"See the [How to add smoothing and band-limits to optimized controls](https://docs.q-ctrl.com/boulder-opal/user-guides/how-to-add-smoothing-and-band-limits-to-optimized-controls) user guide for more details about incorporating filters into your graphs.\n",
"\n",
"From this filtered $\\omega_c$ signal, we will also create signals representing the different terms in the Hamiltonian, such as $g_a^2/(2\\Delta_a)$, $g_b^2/(2\\Delta_b)$, and $\\tilde g$.\n",
"We will also add constant operators to the Hamiltonian, representing its static terms—in this case the fixed frequency terms for each qubit."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"from qctrlvisualizer import plot_controls\n",
"\n",
"from qctrl import Qctrl\n",
"\n",
"# Starting a session with the API.\n",
"qctrl = Qctrl()"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# Define system parameters.\n",
"\n",
"# Physical parameters.\n",
"C_a = 70 # fF\n",
"C_b = 72 # fF\n",
"C_ac = 4 # fF\n",
"C_bc = 4.2 # fF\n",
"C_c = 200 # fF\n",
"C_ab = 0.1 # fF\n",
"omega_a = 2 * np.pi * 4e9 # Hz\n",
"omega_b = 2 * np.pi * 4e9 # Hz\n",
"eta = C_ac * C_bc / (C_ab * C_c)\n",
"gate_duration = 100e-9 # s"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# Create graph.\n",
"graph = qctrl.create_graph()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"# Standard qubit operators.\n",
"iswap = np.array(\n",
" [\n",
" [1.0, 0.0, 0.0, 0.0],\n",
" [0.0, 0.0, 1j, 0.0],\n",
" [0.0, 1j, 0.0, 0.0],\n",
" [0.0, 0.0, 0.0, 1.0],\n",
" ]\n",
")\n",
"\n",
"# 2-qubit operators.\n",
"sigma_z_a = graph.pauli_kronecker_product([(\"Z\", 0)], 2)\n",
"sigma_z_b = graph.pauli_kronecker_product([(\"Z\", 1)], 2)\n",
"sigma_plus_minus = graph.pauli_kronecker_product([(\"P\", 0), (\"M\", 1)], 2)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"# Create basic piecewise-constant signal.\n",
"omega_c_raw_values = (\n",
" (5.4 - 0.34 * np.array([0, 1, 2, 3, 2, 1, 0])) * 2 * np.pi * 1e9\n",
") # Hz\n",
"omega_c_raw = graph.pwc_signal(\n",
" duration=gate_duration, values=omega_c_raw_values, name=\"omega_c_raw\"\n",
")\n",
"\n",
"# Apply a sinc filter (and rediscretize) to simulate\n",
"# the effect of control line imperfections.\n",
"omega_c = graph.utils.filter_and_resample_pwc(\n",
" pwc=omega_c_raw,\n",
" cutoff_frequency=2 * np.pi * 0.1e9,\n",
" segment_count=100,\n",
" name=\"omega_c\",\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"# Calculate derived couplings in the Hamiltonian terms.\n",
"Delta_a = omega_a - omega_c\n",
"Delta_b = omega_b - omega_c\n",
"Delta = 2 / (1 / Delta_a + 1 / Delta_b)\n",
"Sigma_a = omega_a + omega_c\n",
"Sigma_b = omega_b + omega_c\n",
"Sigma = 2 / (1 / Sigma_a + 1 / Sigma_b)\n",
"\n",
"g_a = 0.5 * C_ac * graph.sqrt(omega_a * omega_c / (C_a * C_c))\n",
"g_b = 0.5 * C_bc * graph.sqrt(omega_b * omega_c / (C_b * C_c))\n",
"g_tilde = (\n",
" 0.5\n",
" * (omega_c * eta / (2 * Delta) - omega_c * eta / (2 * Sigma) + eta + 1)\n",
" * C_ab\n",
" * graph.sqrt(omega_a * omega_b / (C_a * C_b))\n",
")\n",
"\n",
"detuning_a = g_a**2 / (2 * Delta_a)\n",
"detuning_b = g_b**2 / (2 * Delta_b)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"# Build the system's Hamiltonian from its terms.\n",
"\n",
"detuning_a_term = detuning_a * sigma_z_a\n",
"# This is equivalent to using graph.pwc_operator:\n",
"# detuning_a_term = graph.pwc_operator(detuning_a, sigma_z_a).\n",
"\n",
"detuning_b_term = detuning_b * sigma_z_b\n",
"coupling_term = g_tilde * 2.0 * graph.hermitian_part(sigma_plus_minus)\n",
"\n",
"# Tensors and arrays are considered constant operators.\n",
"fixed_frequency_a_term = omega_a * sigma_z_a / 2\n",
"\n",
"# We can also explicity declare constant PWC operators.\n",
"fixed_frequency_b_term = graph.constant_pwc_operator(\n",
" operator=omega_b * sigma_z_b / 2, duration=gate_duration\n",
")\n",
"\n",
"# Add all operators into full Hamiltonian.\n",
"hamiltonian = (\n",
" detuning_a_term\n",
" + detuning_b_term\n",
" + coupling_term\n",
" + fixed_frequency_a_term\n",
" + fixed_frequency_b_term\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"# Compute the time evolution operators for the system.\n",
"# We provide a name to extract them after you execute the graph.\n",
"time_evolution_operators = graph.time_evolution_operators_pwc(\n",
" hamiltonian=hamiltonian,\n",
" sample_times=np.linspace(0, gate_duration, 10),\n",
" name=\"time_evolution_operators\",\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
" 0%| | 0/100 [00:00"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Final time evolution operator:\n",
"[[-0.126-0.992j 0. +0.j 0. +0.j 0. +0.j ]\n",
" [ 0. +0.j -0.169-0.058j 0. +0.984j 0. +0.j ]\n",
" [ 0. +0.j -0. +0.984j -0.169+0.058j 0. +0.j ]\n",
" [ 0. +0.j 0. +0.j 0. +0.j -0.126+0.992j]]\n"
]
}
],
"source": [
"plot_controls(\n",
" plt.figure(),\n",
" {\n",
" \"$\\omega_c^\\mathrm{raw}$\": result.output[\"omega_c_raw\"],\n",
" \"$\\omega_c^\\mathrm{filtered}$\": result.output[\"omega_c\"],\n",
" },\n",
")\n",
"plt.show()\n",
"\n",
"print(\"Final time evolution operator:\")\n",
"print(np.round(result.output[\"time_evolution_operators\"][\"value\"][-1], 3))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.9.13"
}
},
"nbformat": 4,
"nbformat_minor": 4
}