Lesson 4: Writing your first Qiskit Code

H Hannan

What is Qiskit
Back to The Qiskit Home Page

This lesson will be a step-by-step guide in which we will build basic quantum circuits, apply common quantum gates and visualize their operation. We will then simulate circuit execution using Qiskit Aer backends to validate quantum behaviour before hardware deployment. Finally, we interface real quantum processors from providers like IBM Quantum, execute code on these quantum devices and analyze experimental results.

Importing Key Qiskit Modules

With Qiskit installed, we can now import Python modules that enable quantum programming:

from qiskit import QuantumCircuit, transpile, assemble, execute 
from qiskit import Aer, IBMQ
from qiskit.visualization import plot_histogram

This imports functionality for:

  • QuantumCircuit: Building quantum circuit diagrams
  • Transpilation and Assembly: Compiling quantum circuits
  • Executing: Running circuits on quantum backends
  • Aer: Accessing simulator backends
  • IBMQ: Accessing real quantum hardware
  • Visualization: Plotting histogram output

Later we will import additional modules like Ignis for error mitigation.

Constructing Your First Quantum Circuit

Programming a quantum algorithm involves describing a quantum circuit consisting of qubits, classical bits, and quantum operations manipulating them. Let’s now code our first Qiskit quantum circuit.

Initializing Quantum and Classical Registers

Every Qiskit circuit requires:

  • Quantum registers – To hold qubits
  • Classical registers – To hold regular bits
qr = QuantumRegister(2) # 2 qubit quantum register
cr = ClassicalRegister(2) # 2 bit classical register  

We combine both into a QuantumCircuit:

circuit = QuantumCircuit(qr, cr) # Create quantum circuit

Adding Quantum Gates and Measurements

With registers defined, we can now apply quantum operations:

circuit.h(qr[0]) # Apply H (Hadamard) Gate to qubit 0
circuit.cx(qr[0], qr[1])  # Apply CX (CNOT) gate
circuit.measure(qr, cr) # Collapse qubit to classical bit  

The h gate (Hadamard) creates a uniform superposition. The cx gate (CNOT) entangles qubits. Measure collapses this state to a binary classical state.

Learn about Quantum Logic Gates Here

We can visualize the circuit using circuit.draw() which outputs the logic diagram. Feel free to add more qubits, gates or measurements in the blank circuit template!

Simulating Quantum Circuits

To understand this quantum circuit’s actual behaviour, we will next simulate it computationally using Qiskit before deploying it on real hardware.

Compiling Circuits for Simulation

Running simulations requires two steps in Qiskit:

  1. Transpilation: Circuit optimization for the target device
  2. Assembly: Translation to executable Qiskit Quantum Assembly Language (QASM)
backend = Aer.get_backend('qasm_simulator') # Quantum simulator
compiled_circuit = transpile(circuit, backend) # Optimization
qobj = assemble(compiled_circuit) # Assembly into QASM  

Executing Circuit Simulation

With the compiled QASM object qobj, we can now process the simulation:

job = backend.run(qobj)  
result = job.result()

Analyzing Simulation Outcomes

Following completion, we can extract and analyze the result data:

counts = result.get_counts(circuit)
plot_histogram(counts) 

This plots a histogram with measured qubit state probabilities, allowing us to validate circuit behaviour prior to hardware. We can tweak the circuit to match the expected outcomes.

Advanced Simulation Concepts

Additionally, Qiskit Aer enables:

  • Debugging: Print statements to log qubit states
  • Noise models: Simulating real hardware noise
  • Snapshot: Intermediate state outputs
  • Additional algorithms like QASM, matrix or state vector simulation

Thorough simulation is key before running quantum circuits on actual devices.

Executing Quantum Circuits on Real Chips

Once a circuit is completely simulated and debugged, we can execute it on real quantum hardware using IBM Quantum services.

Accessing the IBMQ Quantum Devices

First, we initialize access to the IBMQ devices and pick a backend chip:

IBMQ.load_account()  
provider = IBMQ.get_provider('ibm-q')  

backend = provider.get_backend('ibmq_16_melbourne')

Compiling and Running Circuits on Quantum Hardware

With the backend initialized, we can transpile, assemble and execute our circuit:

compiled_circuit = transpile(circuit, backend) # Re-optimize
job = execute(compiled_circuit, backend, shots=1024) 

result = job.result()  
real_counts = result.get_counts(circuit)

The shots argument allows sampling multiple runs for probabilistic quantum output.

Analyzing Outcomes from Real Chips

Finally, we can visualize the real hardware results:

plot_histogram(real_counts)

Comparing these to simulation results, we may notice greater deviations from the ideal case due to hardware noise. We can mitigate this using techniques like error correction.

Accessing real quantum computers is an invaluable quantum programming practice for today’s developers and researchers. Cloud services have profoundly democratized early hands-on quantum computation.

Troubleshooting Quantum Code in Qiskit

As with any programming, you may encounter issues debugging your quantum code. Some common problems and their solutions:

Debugging Circuits

  • Use Aer snapshot, and print statements to log intermediate qubit states
  • Compare simulator noise models to pinpoint error origins
  • Draw a circuit diagram throughout the flow to visualize the logic

Optimization Issues

  • Ensure transpiler optimization is set correctly for the backend
  • Break down circuits into smaller subcircuits where possible

Access and Job Failures

  • Check chosen backend configuration, availability and limits on shots or runtime
  • Retry failed jobs in case of transient device calibration issues

Measurement Statistics

  • Increase the number of shots for better sampling of qubit probabilities
  • Analyze all output count data to explain unexpected collapsed states

With these troubleshooting tips and questions for community forums, you can diagnose most quantum program errors.

Summary

We have covered end-to-end development and execution of quantum algorithms using Qiskit – from environment setup to simulation, debugging, real hardware execution and analyzing results. This should provide a comprehensive basis to start programming the incredible computing machines of the future!

Leave a Comment