Qiskit – Practical open source framework for quantum computing

Smart Toolbox

Article from Issue 265/2022
Author(s): , Author(s): , Author(s): , Author(s): , Author(s): , Author(s): , Author(s): , Author(s): , Author(s):

Qiskit is an open source framework that aims to make quantum computing technology both understandable and ready for production.

IBM is working on increasing the performance of the interaction between hardware and software. The milestones [1] planned for this effort are shown in the IBM Quantum Development Roadmap (Figure 1). To benefit from IBM Quantum hardware, the software and infrastructure must support a wide range of requirements and experiences. This need for versatility requires tools for different types of developers.

Figure 1: IBM has laid out exactly what goals it wants to achieve, and when, in the IBM Quantum Development Roadmap.

Qiskit (think "kiss kit") is a freely available software development kit (SDK) for working with quantum computers at the level of pulses, circuits, or application modules. At least, that's what the SDK home page says. Since its first publication in 2017, the project has grown and undergone some fundamental changes.

Figure 2 provides an overview of Qiskit and the major projects within the Qiskit community. In addition to this main Qiskit effort are numerous other projects that include the Qiskit name and form an important part of the Quantum open source landscape [2]. (See the "Open Source in Quantum Computing" box.)

Open Source in Quantum Computing

Because Qiskit is open source software, other companies can implement an interface to their quantum computers. For example, Alpine Quantum Technologies (AQT [3]) is working on a general-purpose quantum computer. To access its back end, you just need to create an account and install the Qiskit AQT provider (Listing 1). Dr. Albert Frisch, senior research engineer at AQT, likes the open source spirit surrounding quantum computing: "Building general-purpose quantum computers is complex, but thanks to Qiskit, customers have easy access to our systems and can gain experience at different levels, from novice to expert."

Listing 1

Installing the AQT Provider

from qiskit_aqt_provider import AQTProvider
aqt = AQTProvider('MY_TOKEN')
print(aqt.backends())
sim_backend = aqt.backends.aqt_qasm_simulator
Figure 2: This overview shows the largest and most stable projects in the Qiskit environment.

Many of the core Qiskit capabilities are implemented in the form of Qiskit Terra [4], the largest component in the Qiskit ecosystem. Many of the other components have dependencies on Terra. Table 1 lists some important Qiskit modules.

Table 1

Qiskit Modules at a Glance

Module

Description

qiskit.circuit

Quantum circuits for initializing and editing objects for flow control, registers, circuits, instructions, gates, and parameters.

qiskit.circuit.library

Library with numerous predefined circuits, directives, and gates as basic building blocks for circuit-based quantum computations.

qiskit.algorithms

Collection of quantum algorithms, such as Grover's search algorithm [5], Variational Quantum Eigensolver (VQE) [6], or Quantum Approximate Optimization Algorithm (QAOA) [7].

qiskit.opflow

Collection of operators as a lingua franca for the theory and implementation of quantum algorithms and applications.

qiskit.primitives

Collection of classes (such as Sampler and Estimator) to include basic operations provided by quantum hardware and simulators.

qiskit.transpiler

Transpilers to help rewrite quantum circuits to match the topology of a particular quantum computer.

qiskit.visualization

Tools used to visualize quantum circuits and results of experiments.

Qiskit comes with a number of components that focus on specific use cases. For instance, Qiskit Nature aims to solve scientific problems using quantum algorithms. You'll find tools intended to address basic and excited states of molecular structures.

Qiskit Machine Learning provides tools for a number of applications in the Quantum Machine Learning research area. These tools include quantum neural networks (QNNs), quantum generative adversarial networks (QGANs), and quantum kernels. The TorchConnector class provides an interface to PyTorch and lets users combine classical artificial intelligence workflows with quantum-based approaches. Qiskit Finance, on the other hand, offers quantum-based approaches to solving typical problems in finance, for example portfolio optimization, risk analysis, and price evaluation of financial options. Qiskit Optimization is a collection of tools designed to address optimization problems using quantum algorithms.

Qiskit also includes components that operate at a lower level in the development stack. The Qiskit Experiments library, for instance, is used to run characterization, calibration, and verification experiments on real qubits. Qiskit Metal provides engineers and scientists with graphical support for developing chips for superconducting quantum computers. Another component called Qiskit Dynamics is used to build, transform, and solve time-dependent quantum systems. Qiskit Aer delivers simulators that allow you to simulate quantum computations on classical computers.

Running quantum circuits on real quantum computers requires an interface between Qiskit and the appropriate hardware vendor. This hardware interface is already available from a number of vendors, including IBM, AQT, and IonQ.

Quantum Composer

IBM Quantum Composer provides an easy entry point to visually define quantum circuits online using drag and drop. All you need is a free IBM Quantum account [8]. You can export the result of your work directly as code. Figure 3 shows a GHZ state for three qubits, where all are maximally entangled (see the article entitled "Cat's Dilemma" elsewhere in this issue).

Figure 3: With the help of IBM Quantum Composer, you can easily map quantum circuits online.

In the IBM Quantum Lab, you can write your code directly or edit previously exported code. Listing 2 shows how to create a GHZ state as an example: Superpose qubit 0 and then interleave the other qubits with qubit 0 using CX (controlled-NOT) gates. The results from Figure 3 were computed using the Aer simulator and confirm that a GHZ condition exists.

Listing 2

Creating a GHZ State

from qiskit import QuantumCircuit, execute, Aer, QuantumRegister
qc = QuantumCircuit( 3, 3
 )
qc.h( 0 )
qc.cx( 0, 1 )
qc.cx( 0, 2 )
qc.measure_all()
simulator = Aer.get_backend( 'qasm_simulator' )
job = execute( qc, simulator, shots=4000 )
result = job.result()
counts=result.get_counts()
print(counts)

Instead of simulating the circuit, you can run it on a quantum computer. One of the available IBM quantum computers can provide the back end for the example in Listing 3. The results are not quite as clear due to the noise on the physical system. The 4000 measurements also include some measurements that do not return either |000> nor |111> – even though the majority still show the correct states. We will see how to avoid such mistakes later on.

Listing 3

Quantum Computer as Back End

from qiskit import IBMQ
# Query the freely available quantum computers
provider = IBMQ.load_account()
provider = IBMQ.get_provider( hub='ibm-q' )
print(provider.backends())
# Run on a physical quantum computer
device = provider.get_backend( 'ibmq_lima' )
job = execute( qc, backend=device, shots=4000 )
result = job.result()
counts=result.get_counts()
print(counts)

Qiskit Runtime

Quantum computers are ideal for solving a few specific types of problems, but they aren't designed to manage the whole process. Instead, a quantum computer typically works together with classical computer; the classical computer pre-processes input or post-processes the result, sometimes managing several iterations of quantum processing steps. A typical scenario would be a legacy system running locally and accessing a quantum system in the cloud. However, connecting a cloud-based quantum system with a local classical computer is quite inefficient, because of the latency associated with communicating over the Internet. Qiskit Runtime [9] offers a new paradigm for running computations on quantum computers. The Qiskit Runtime execution environment allows hybrid algorithms that mix legacy and quantum computations to all run directly in the cloud

The runtime shows improvements for both hybrid and more general algorithms. You can access Qiskit Runtime using an IBM Quantum account. Alternatively, you can create a Qiskit Runtime instance in the IBM cloud.

To create an instance, run PIP to install both Qiskit (package qiskit) and the Qiskit Runtime IBM Client (qiskit-ibm-runtime).

First, you need to create the general GHZ state for n qubits (Listing 4, lines 6 and 7) – the example with three qubits was given earlier in this article. Then apply a bitwise XOR with the number 1 to this state to flip the first qubit (line 8). Then use Sampler (from line 6) to determine the quasi-probabilities of the circuit.

Listing 4

Quasi-Probabilities of the Circuit

01 from qiskit.circuit.library import XOR
02 from qiskit_ibm_runtime import QiskitRuntimeService, Sampler
03 N = 5
04 qc2 = QuantumCircuit(N)
05 qc2.h( 0 )
06 for x in range( 1, N ):
07   qc2.cx( 0, x )
08 qc2.compose( XOR(N,1), inplace=True )
09 qc2.measure_all()
10 program_inputs = {
11   "circuits": qc2,
12   "circuit_indices": [0],
13 }
14 service = QiskitRuntimeService( channel="ibm_quantum", token=IBM-Quantum-API-Key)
15 options = { 'backend_name': 'ibmq_lima' }
16 job = service.run(
17   program_id="sampler",
18   options=options,
19   inputs=program_inputs,
20 )
21 print(job.result())

Qiskit at Work

The Qiskit team is always looking for real-world examples that demonstrate quantum principles. See the box entitled "Games" for some Qiskit-based quantum computing games that are available online. A deeper example is the work of Team Quantimize [11], a group of five quantum technology students who have already participated in several quantum challenges, such as the Qiskit Global Hackathon 2021. However, their biggest project to date was the Deloitte Quantum Climate Challenge 2022. The task was to optimize flight routes in order to protect the earth's atmosphere to the greatest extent possible, since burning kerosene has a different effect at different locations. Team Quantimize used atmospheric data from the German Aerospace Center (DLR) and information on minimum distances from German Air Traffic Control (DFS) to find an optimized, quantum-based solution.

Games

Games are a helpful way to approach a new technology and build an understanding of it. One example is the quantum coin game, which makes use of the principles of superposition and interference. Actually, the quantum coin game isn't much of a game at all, although it is an excellent way to illustrate the use of quantum computing. On a classical computer, the coin game gives each of the two players a 50 percent chance of winning. On a quantum computer, the results are a bit more one-sided. A qubit represents a coin that two players (Alice and Bob) take turns either flipping or leaving in its current position. Flipping corresponds to applying an X-gate to the qubit; leaving it corresponds to the state of an LD-gate. The procedure is always the same: At the beginning, heads is on top, which is equivalent to the state |1>. Alice starts and three rounds are played without seeing the results. In the end, Alice wins for heads, and Bob wins for tails.

Classically, the game amounts to a series of coin flips, and each player has an equal chance. However, if you play the game on a quantum computer, the outcome is quite different. Alice can additionally apply a Hadamard (H) gate to put the qubit into a superposition, so that the coin is effectively both heads and tails at once. Bob's move then no longer has any effect, since neither the application of the X-gate nor the LD-gate significantly changes the state of the qubit representing the coin. If Alice now applies an H-gate again in her last move, which resolves the superposition, she wins with a probability of 100 percent. Figure 4 shows the course of the game using the Bloch sphere.

Figure 4: In a quantum coin game, the odds are no longer equal for the two players.

Another, somewhat more complex example is the GHZ game, which takes advantage of the entanglement of a GHZ circuit. You'll find the details and exact instructions for both games, including Jupyter notebooks, on the GitHub page of one of the authors [10] if you are interested.

The idea was to map the atmospheric data onto a map of qubits, all in superposition. Using the Quantum Approximate Optimization Algorithm (QAOA), the students then determined the best flight path. QAOA is an iterative process that involves the interaction of a legacy computer with a quantum computer to solve an optimization problem (Figure 5). The algorithm sorts the qubits into |0> and |1> states, so that the dividing line between them results in the flight path.

Figure 5: QAOA solves optimization problems as an iterative process in which a legacy computer and a quantum computer work together.

For this challenge, the team was able to directly apply the existing QAOA algorithm to their problem and convert it into a quantum circuit. Listing 5 demonstrates the core element of the optimization, where cost_grid represents the discretized atmospheric data.

Listing 5

QAOA to a Quantum Circuit

import qiskit
def run_QAOA( cost_grid, backend=qiskit.Aer.get_backend( 'qasm_simulator' ) ):
  # Generate cost function from a cost matrix (cost_grid);
  # function written in-house
  function, coeffs = construct_function( cost_grid )
  # Create quadratic program; function written in-house,
  # nutzt qiskit.optimization.QuadraticProgram
  qp = generate_QP( coeffs, len( cost_grid ) )
  # Initialize backend and optimization algorithm
  qins = qiskit.utils.QuantumInstance( backend=backend, shots=1000, seed_simulator=123 )
  meo = qiskit_optimization.algorithms.MinimumEigenOptimizer \
        ( min_eigen_solver=qiskit.algorithms.QAOA( reps=1, quantum_instance=qins) )
  # Solve problem with built-in QAOA algorithm
  result = meo.solve(qp)

Team Quantimize used Qiskit to simulate its quantum circuit and then run it on an IBM quantum computer. Using brute force, it was possible to confirm that both the simulation and the quantum computer produced matching results.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Quantum Computing

    The peculiar world of quantum mechanics points the way to a whole new kind of computer. If you're wondering how quantum computers work, we'll give you an inside view.

  • Quantum Computing and Encryption

    The encryption methods we use today are no match for tomorrow's quantum computers. We'll show you why and what's ahead for cryptography in the post-quantum era.

  • Google and NASA Partner in Quantum Computing Project

    Vendor D-Wave scores big with a sale to NASA's Quantum Intelligence Lab.

  • Program Library for Quantum Simulation Leaps to Version 0.9.1

    If you'll pardon the pun, the Libquantum C library has now leaped from version 0.2.4 to 0.9.1 after three years of seeming inactivity. The new version includes a new API which gives users the ability to simulate quantum mechanics.

  • NEWS

    Kubernetes 1.8 announced, final Ubuntu Desktop 17.10 Beta arrives, Linus Torvalds invites attackers to join the kernel community, Oracle donates Java EE to the Eclipse Foundation, and Microsoft is building a programming language for quantum computers. 

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News