# Lesson 6: Visualizing Quantum States and Building Quantum Circuits

H Hannan

Being able to reason about how qubits evolve as they pass through quantum gate operations and assume different quantum states is vital for debugging and validating quantum programs. Qiskit provides versatile visualization capabilities at various levels – from high-level quantum circuits all the way down to numerical quantum state vectors. In this section, we will leverage these rich quantum state visualization tools to understand qubit evolution before undertaking a hands-on exercise building and simulating a simple quantum circuit.

## Visualizing Quantum Circuits

The quantum circuit architecture captures the bird’s eye view of the programmed sequence of quantum operations. We can visualize the circuit layout by calling the draw() method:

``qc.draw() # Generate an interactive circuit diagram``

This outputs a rendering of the gates being applied to qubits and classical bits in order. Alternatively, we can generate images for integration into documents:

``````%matplotlib inline
qc.draw(output='mpl') # Output circuit as a Matplotlib figure``````

The interactive circuit visualization allows traversing the programmed quantum algorithm workflow step-by-step before compilation or execution. We can click elements to highlight qubit state changes through each gate operation as well as hover over components for insights into parameters. This aids in debugging circuit construction modularly from both system-level and functional perspectives.

In addition to standard graphics, Qiskit also supports exporting the circuit as LaTeX markup for scientific publications as well as in IBM quantum assembly format for reuse. Hence circuits provide a quick yet precise visualization of the high-level qubit state manipulation.

#### Tracking Underlying Quantum States

More fundamentally, we can also directly analyze the full quantum state of qubits as they evolve in a simulation backend using numerical statevector or density matrix representations.

The statevector fully describes the quantum system by encoding the probability amplitude and phase corresponding to each basis state configuration the qubits can assume. We can extract and plot snapshots of the statevector after every applied gate operation to understand exactly how the qubit state gets modified mathematically.

``````simulator = Aer.get_backend('statevector_simulator')
result = execute(qc, backend =simulator).result()
statevector = result.get_statevector()
print(statevector)

from qiskit.tools.visualization import plot_bloch_multivector as plot_bloch
circuit.draw(output='mpl')
plot_bloch(statevector)``````

Similarly, the density matrix is useful for visualizing quantum evolution for mixed states as well. Matrix grids and complex phase charts help build deeper intuition into the qubit manipulation.

``from qiskit_aer import *simulator = Aer.get_backend('unitary_simulator')result = execute(qc, backend =simulator).result()unitary = result.get_unitary()print(unitary)``

These low-level mathematical visualizations thus enable reasoning about the precise quantum state flows and transformations underlying circuit operations.

Combined with the high-level circuit architecture, Qiskit’s quantum state visualization toolkit allows both overarching and detailed views into the qubit dynamics to accelerate debugging and monitoring. We will leverage both these perspectives in our next exercise.

## Practical Exercise: a Simple Bell State Quantum Circuit

Let’s now apply the quantum circuit programming concepts we have learned by iteratively constructing, simulating and visualizing outputs of a simple Bell state generation circuit.

The workflow will be:

1. Initialize a quantum circuit
3. Simulate circuit execution
4. Visualize final quantum states

Bell states represent a set of four maximally entangled quantum states involving two qubits. These states exemplify quantum entanglement’s profound implications, where qubits remain interconnected regardless of distance. Essential in quantum computing, cryptography, and teleportation, Bell states challenge classical mechanics, showcasing quantum superposition and the non-locality of quantum mechanics.

#### Importing Qiskit Modules

We first import the elements needed:

``````from qiskit import QuantumCircuit, execute, Aer
from qiskit.visualization import plot_state_qsphere``````

This allows building circuits, simulation tools and state visualization.

#### Constructing the Quantum Circuit

Next, we initialize and construct a 2-qubit quantum circuit designed to generate one of the Bell states:

``````qc = QuantumCircuit(2, 2) # Initialize with 2 qubits and 2 classical bits

qc.h(0) # Apply hadamard to qubit 0
qc.cx(0, 1) # Then CX to entangle qubits

qc.measure([0,1], [0,1]) # Measure qubits onto classical bits``````

This first applies a Hadamard on qubit 0 to create a superposition. It then uses a CX gate to entangle it with qubit 1. Finally, it measures both qubits onto separate classical bits.

#### Compiling and Simulating the Circuit

With the Bell state generation circuit defined, we can now compile it for and execute a simulation using Aer’s high-performance qasm_simulator:

``````simulator = Aer.get_backend('qasm_simulator')
compiled_qc = transpile(qc, simulator) # Compile for backend
qobj = assemble(compiled_qc)
results = simulator.run(qobj).result() # Execute simulation``````

Analyzing and Visualizing Simulation Results With results from the simulator, we can now recover measurements statistics and plot output visualizations:

``````from qiskit.tools.visualization import plot_histogram

counts = results.get_counts(qc)
plot_histogram(counts) # Visualize measured qubit states``````

The histogram charts the probability distribution over various final qubit states, allowing validation of expected Bell state generation. The expected results can be seen below:

We could further visualize the full quantum state vector evolution underlying the simulated circuit using tools like plot_statevector. By iterating on analysis and refinement, we can systematically debug quantum programs.

Thus combining Qiskit’s instrumentation of the high-level quantum circuit architecture and low-level quantum state manipulations facilitates rapid quantum application development.