Author: Rushiraj Gadhvi ([email protected])
This notebook serves as a comprehensive guide to plotting quantum circuits using QuTiP-QIP's TextRenderer. It explores the various customization options available to users for creating and modifying plots.
import qutip
import numpy as np
from qutip import Qobj
from qutip_qip.operations import rx
from qutip_qip.circuit import QubitCircuit
qc = QubitCircuit(2, num_cbits=1)
qc.add_gate("CNOT", controls=0, targets=1)
qc.add_gate("SNOT", targets=1)
qc.add_gate("ISWAP", targets=[0, 1])
qc.add_measurement("M0", targets=1, classical_store=0)
qc.draw("text")
┌──────┐ ┌──────┐ ┌───────┐ ┌───┐ q1 :───┤ CNOT ├──┤ SNOT ├──┤ ├──┤ M ├─── └───┬──┘ └──────┘ │ │ └─╥─┘ │ │ │ ║ q0 :───────█───────────────┤ ISWAP ├────║───── └───────┘ ║ ║ c0 :════════════════════════════════════╩═════
qc.draw("text", wire_label=["some_name", "some_long_name", "long_long_name"])
┌──────┐ ┌──────┐ ┌───────┐ ┌───┐ long_long_name :───┤ CNOT ├──┤ SNOT ├──┤ ├──┤ M ├─── └───┬──┘ └──────┘ │ │ └─╥─┘ │ │ │ ║ some_long_name :───────█───────────────┤ ISWAP ├────║───── └───────┘ ║ ║ some_name :════════════════════════════════════╩═════
qc.draw("text", end_wire_ext=0)
┌──────┐ ┌──────┐ ┌───────┐ ┌───┐ q1 :───┤ CNOT ├──┤ SNOT ├──┤ ├──┤ M ├─ └───┬──┘ └──────┘ │ │ └─╥─┘ │ │ │ ║ q0 :───────█───────────────┤ ISWAP ├────║─── └───────┘ ║ ║ c0 :════════════════════════════════════╩═══
qc.draw("text", end_wire_ext=5)
┌──────┐ ┌──────┐ ┌───────┐ ┌───┐ q1 :───┤ CNOT ├──┤ SNOT ├──┤ ├──┤ M ├────── └───┬──┘ └──────┘ │ │ └─╥─┘ │ │ │ ║ q0 :───────█───────────────┤ ISWAP ├────║──────── └───────┘ ║ ║ c0 :════════════════════════════════════╩════════
qc.draw("text", gate_pad=3)
┌──────────┐ ┌──────────┐ ┌───────────┐ ┌───────┐ q1 :───┤ CNOT ├──┤ SNOT ├──┤ ├──┤ M ├─── └─────┬────┘ └──────────┘ │ │ └───╥───┘ │ │ │ ║ q0 :─────────█─────────────────────┤ ISWAP ├──────║─────── └───────────┘ ║ ║ c0 :══════════════════════════════════════════════════╩═══════
qc = QubitCircuit(2)
qc.add_gate("H", targets=[0])
qc.add_gate("H", targets=[1], arg_label="hadamard gate")
qc.draw("text")
┌───────────────┐ q1 :───┤ hadamard gate ├─── └───────────────┘ ┌───┐ q0 :───┤ H ├─────────────── └───┘
def user_gate1(arg_value):
# controlled rotation X
mat = np.zeros((4, 4), dtype=np.complex)
mat[0, 0] = mat[1, 1] = 1.0
mat[2:4, 2:4] = rx(arg_value).full()
return Qobj(mat, dims=[[2, 2], [2, 2]])
def user_gate2():
# S gate
mat = np.array([[1.0, 0], [0.0, 1.0j]])
return Qobj(mat, dims=[[2], [2]])
qc = QubitCircuit(3)
qc.user_gates = {"CTRLRX": user_gate1, "S": user_gate2}
# qubit 1 controls qubit 0
qc.add_gate("CTRLRX", targets=[1, 0], arg_value=np.pi / 2)
# qubit 2 is target of S gate
qc.add_gate("S", targets=[2])
qc.draw("text")
┌───┐ q2 :───┤ S ├──────── └───┘ ┌────────┐ q1 :───┤ ├─── │ │ │ │ q0 :───┤ CTRLRX ├─── └────────┘
qutip.about()
QuTiP: Quantum Toolbox in Python ================================ Copyright (c) QuTiP team 2011 and later. Current admin team: Alexander Pitchford, Nathan Shammah, Shahnawaz Ahmed, Neill Lambert, Eric Giguère, Boxi Li, Simon Cross, Asier Galicia, Paul Menczel, and Patrick Hopf. Board members: Daniel Burgarth, Robert Johansson, Anton F. Kockum, Franco Nori and Will Zeng. Original developers: R. J. Johansson & P. D. Nation. Previous lead developers: Chris Granade & A. Grimsmo. Currently developed through wide collaboration. See https://github.com/qutip for details. QuTiP Version: 5.2.0.dev0+4e6ded9 Numpy Version: 2.2.5 Scipy Version: 1.15.2 Cython Version: 3.0.12 Matplotlib Version: 3.10.1 Python Version: 3.12.0 Number of CPUs: 4 BLAS Info: Generic INTEL MKL Ext: None Platform Info: Linux (x86_64) Installation path: /home/runner/miniconda3/envs/test-environment-v5/lib/python3.12/site-packages/qutip Installed QuTiP family packages ------------------------------- qutip-qtrl: 0.2.0.dev0+acb71a0 qutip-jax: 0.1.1.dev5 qutip-qip: 0.5.0.dev0+a6d68ca ================================================================================ Please cite QuTiP in your publication. ================================================================================ For your convenience a bibtex reference can be easily generated using `qutip.cite()`