QISKIT: 3 important VISUALIZATION tools

H Hannan

Updated on:

QISKIT: 3 important VISUALIZATION tools
Back to The Qiskit Home Page

When building quantum circuits and systems on qiskit, it is important to be able to visualise what is actually happening to the qubits and how quantum gates affect them. Here are 4 useful ways to visualise them. Learn more about here.

Qiskit State vector:

A statevector is a mathematical representation that describes the quantum state of a physical system. Specifically, a statevector encapsulates all the information about the probabilities of the quantum system being in each possible state. In quantum mechanics, the state of a system cannot be specified precisely as it can in classical physics. Instead, there is inherent uncertainty about the exact properties before measurement. The statevector encodes this fuzzy probabilistic description.

To code an output as a statevector, use this code:

circuit = QuantumCircuit(1,1)
circuit.x(0)
simulator = simulator = Aer.get_backend('statevector_simulator')
result = execute(circuit, backend =simulator).result()
statevector = result.get_statevector()
print(statevector)

This can then be visualised using:

%matplotlib inline
circuit.draw(output='mpl')
plot_bloch(statevector)

Qiskit Execution measurement:

In quantum programming, the execution-measurement pattern refers to the sequence of executing quantum operations on qubits and then measuring the qubit states to obtain classical outputs. This quantum-classical cycle is a core part of many quantum algorithms. The execution-measurement pattern leverages the strengths of both quantum and classical computing – using quantum parallelism for computation and classical logic for control flow and output. Mastering this quantum-classical interplay is key to practical quantum algorithms.

To code an output as a histogram, use this code:

circuit = QuantumCircuit(1,1)
circuit.x(0)
circuit.measure([0],[0])
simulator2 = Aer.get_backend('qasm_simulator')
result2 = execute(circuit, backend = simulator2).result()
plot_histogram(result2.get_counts(circuit))

Qiskit Matrix:

Matrices and matrix operations are ubiquitous in quantum programming as they provide a convenient way to represent quantum states, operators, and transformations in code. Quantum computing essentially leverages linear algebra, so matrices translate the math directly into practical software implementations.

To code an output as a matrix, use this code:

circuit = QuantumCircuit(1,1)
circuit.x(0)
simulator = simulator = Aer.get_backend('unitary_simulator')
result = execute(circuit, backend =simulator).result()
unitary = result.get_unitary()
print(unitary)

Thing to note: with all of these lines of code, it is important to remember to import the right Python library. These are the ones you will need:

from qiskit import *
from qiskit.tools.visualization import plot_bloch_multivector as plot_bloch
from qiskit.tools.visualization import plot_histogram
from qiskit_aer import *

Find out more here.

Leave a Comment