Testing Quantum Programs Locally and Running on Real Chips

H Hannan

Updated on:

Testing Quantum Programs Locally and Running on Real Chips - Bridging the Quantum Theory-Practice Gap
Back to The Qiskit Home Page

Introduction

Developing practical quantum applications requires extensive testing across both simulated and real hardware environments. Local simulation enables controlled debugging of quantum programs, while access to real quantum processors provides invaluable practical validation.

This guide covers end-to-end quantum programming – from constructing circuits to simulating locally using Qiskit up to executing on IBM Quantum’s real chips. Mastering both simulation and hardware execution is key to overcoming the theory-practice gap on the path to quantum advantage.

Quantum Program Simulation Using Qiskit Aer

Thoroughly testing quantum circuits via simulation is an essential first step before real hardware execution. Simulation provides a virtual sandbox for examining quantum program behaviour under ideal conditions. Qiskit Aer enables high-performance simulation of quantum circuits locally.

To install qiskit, check out this easy-to-follow Setup Guide.

Quantum Circuit Simulation

To simulate a quantum circuit using Aer, we:

  1. Construct the quantum circuit in Qiskit
  2. Transpile the circuit for the target simulator
  3. Assemble a simulation job object (Qobj)
  4. Run on the chosen Aer simulator backend
  5. Analyze the results

For example:

from qiskit import QuantumCircuit, transpile, assemble, Aer

# Construct a sample quantum circuit 
qc = QuantumCircuit(2, 2)

# Add some gates
qc.h(0)  
qc.cx(0, 1)
qc.measure([0,1], [0,1])

# Configure the simulator
simulator = Aer.get_backend('qasm_simulator')

# Transpile and assemble the circuit into a Qobj
compiled_qc = transpile(qc, simulator)  
qobj = assemble(compiled_qc)

# Run the simulation
result = simulator.run(qobj).result()
counts = result.get_counts(qc)

# Analyze simulated counts
print(counts)

The simulated count’s output shows the probability distribution of qubit states at the end. By inspecting these results, we can debug the circuit implementation.

Quantum Statevector Simulation

The statevector simulator provides further insight by outputting the system’s full quantum state vector during simulation. This describes the complex probability amplitude and phase of each qubit state.

We can visualize the statevector using Qiskit’s state_city plot:

from qiskit.visualization import plot_state_city

# Aer statevector simulator
simulator = Aer.get_backend('statevector_simulator') 

# Compile and run simulation
statevector = simulator.run(qobj).result().get_statevector()

# Plot the statevector  
plot_state_city(statevector)

Observing the statevector evolution helps debug quantum states through transformations like superposition, entanglement, and interference.

Executing Quantum Programs on Real Chips via IBM Quantum

Once simulations are extensively validated, the next vital step is deployment on real quantum hardware. Accessing actual quantum processors enables practical programming experience and benchmarks.

Configuring IBM Quantum Experience

To run circuits on real chips, we first configure IBM Quantum services in Qiskit:

from qiskit import IBMQ

# Load IBMQ account details
IBMQ.load_account() 

# Initialize IBMQ provider
provider = IBMQ.get_provider('ibm-q')

Choosing a Quantum Processor

With a provider initialized, we examine the properties of available backends to choose a quantum processor suitable for our program:

backend = provider.backends() # Get available backends

# Filter by criteria like qubits, availability, etc. 
backend = provider.get_backend('ibmq_manila')

Executing Circuits on Real Devices

With a backend selected, we transpile and execute our circuit:

from qiskit import execute

# Transpile circuit for backend 
compiled_qc = transpile(qc, backend)  

# Execute on real hardware
job = execute(compiled_qc, backend, shots=1024) 

# Get results
results = job.result()  
counts = results.get_counts()

The shots argument enables sampling multiple runs for probabilistic qubit results. Higher shots improve accuracy.

Analyzing Results and Noise

We can visualize the results just like a simulation. However, real hardware introduces noise that may cause deviations. By comparing simulator fidelity to hardware, we can fine-tune mitigation strategies.

Repeated testing hones circuits and workflows. We quickly transition from textbook knowledge to practical programming proficiency. Real hardware access allows continuously honing skills as technology rapidly evolves.

Conclusion

Testing locally via simulation combined with real hardware execution provides a powerful quantum programming workflow. Simulation offers an idealized testbench, while real chips inject practical validation.

This end-to-end approach bridges the gap from theoretical understanding to practical application. Mastering both tools provides a robust iterative development loop for evaluating and refining quantum programs.

As quantum computers continue maturing from noisy prototypes toward scalable processors, hands-on access for programmers remains critical. Using both simulators and real hardware in conjunction unlocks the full potential of quantum – ushering us steadily into the emerging quantum era.

Join the Qiskit Slack community to engage with the quantum developer ecosystem

Leave a Comment