Visualization
Draw circuit diagrams and plot experimental results, conversion graphs, and more.
API Reference: qbraid.visualization
Draw Circuit Diagrams
qBraid’s circuit_drawer
function takes in any type of supported quantum circuit and draws the corresponding visualization.
Here’s an example using braket
and cirq
:
>>> from qbraid.interface import random_circuit
>>> from qbraid.visualization import circuit_drawer
>>> circuit = random_circuit("braket")
>>> circuit_drawer(circuit)
T : |0| 1 |2|
q0 : -C-C---S-
| |
q1 : -Z-|-Z---
|
q2 : -Z-X-----
T : |0| 1 |2|
>>> circuit = random_circuit("cirq")
>>> circuit_drawer(circuit)
0: ───────────×───Z───
│
1: ───iSwap───×───────
│
2: ───iSwap───Y───H───
Draw OpenQASM 3 circuits
The qasm3_drawer
takes in a string of OpenQASM3 code and outputs an ASCII representation of the described circuit.
from qbraid.visualization import qasm3_drawer
program = """
OPENQASM 3;
include "stdgates.inc";
qubit[2] q;
h q[0];
cx q[0], q[1];
"""
qasm3_drawer(program)
# |---|
# q0---| h |-----■---
# |---| |
# |----|
# q1----------| cx |-
# |----|
Plot Experimental Results
Gather the measurement counts and plot the histogram data for any result
of type qbraid.runtime.GateModelJobResult
:
from qbraid.visualization import plot_histogram
counts = result.raw_counts()
# {'00': 483, '01': 14, '10': 486, '11': 17}
plot_histogram(counts)
Or, using the same measurement counts data, plot the probability distribution:
from qbraid.visualization plot_distribution
plot_distribution(counts)
Or, plot a batch of measurement counts for any list[qbraid.runtime.GateModelJobResult]
:
batch_jobs = device.run_batch([circuit0, circuit1], shots=1000)
batch_results = [job.result() for job in batch_jobs]
batch_counts = [result.measurement_counts() for result in batch_results]
# e.g. [{'0': 136, '1': 864}, {'0': 166, '1': 834}]
plot_histogram(batch_counts)
Using the qBraid runtime job and results primitives, experimental data is returned in a standardized format, facilitating straightforward comparisons and benchmarking of results across different providers and backends.
Plot Transpiler Conversions
Plot all supported conversions between registered program types available through the qbraid.transpiler
:
from qbraid.transpiler import ConversionGraph
from qbraid.visualization import plot_conversion_graph
graph = ConversionGraph()
plot_conversion_graph(graph, legend=True)