Drawing (plain-text circuit diagrams)¶
The fast way to sanity-check what a gate-tuple list actually builds, without running
it or leaving the terminal -- a plain-text diagram, deliberately ASCII-only (-, |,
*), not Unicode box-drawing, so a printed diagram survives any console encoding.
Step 1. A circuit, as text¶
import dense_evolution as de
qasm = 'OPENQASM 2.0; include "qelib1.inc"; qreg q[2]; h q[0]; cx q[0],q[1];'
circuit = de.QASMParser().parse(qasm)
print(de.draw_circuit(circuit.to_tuples(), n_qubits=2))
draw_circuit(circuit, n_qubits) reads the same gate-tuple list every other page's
run_circuit_jit runs, one wire per qubit, gates in the order they appear left to
right -- H on q0, then cx linking q0 (*, the control) to q1 (X, the
target). Nothing to render or save -- the return value is a plain Python string,
printable anywhere.
Details¶
Not the same as plot_circuit: Diagram's plot_circuit draws the
same circuit as an actual saved figure (Quirk-style colored boxes) instead of a
terminal string -- reach for that one when a real image is needed (a docs page, a
report), this one for a quick terminal check.
drawing ¶
Plain-text circuit diagrams -- the fast way to sanity-check what a
gate-tuple list actually builds without running it, the same job
Qiskit's circuit.draw() or Cirq's text diagrams do.
Uses plain ASCII ('-', '|', '*') rather than Unicode box-drawing characters deliberately: a diagram meant to be printed to a terminal or written to a log file needs to survive whatever console encoding the caller happens to have (this was tested against, and would otherwise crash on, a default Windows cp1252 console).
draw_circuit ¶
Render a circuit (in the gate-tuple form run_circuit accepts) as a plain-text diagram, one gate per column, one line per qubit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
list[tuple]
|
Gate ops, as passed to |
required |
n_qubits
|
int
|
Number of qubit rows to draw, must be >= 1 (should cover every
qubit index referenced in |
required |
Returns:
| Type | Description |
|---|---|
str
|
Multi-line diagram, one row per qubit ( |
Examples:
>>> import dense_evolution as de
>>> print(de.draw_circuit([('h', 0), ('cx', 0, 1)], n_qubits=2))
q0: --H----*--
q1: -------X--