Pattern Atlas

Extended Quantum Computing Patterns

Hybrid Module

Aliases:

Tags:

© IARIA, 2023

Intent

How can the implementation of a quantum algorithm requiring both classical and quantum computations be packaged so that it can be integrated into applications?

Context

Quantum algorithms often require classical computation for pre- and post-processing of the quantum computation results [Leymann and Barzen 2020]. This means that almost all quantum algorithms are hybrid. Thus, any implementation of a quantum algorithm has to contain both the quantum and the classical parts for the algorithm to be functional.

Forces

Quantum algorithms typically require a classical computer for some parts of their computation. This means that they can have multiple quantum and classical parts. For example, VQAs, such as VQE and QAOA, alternate between quantum and classical computations [Cerezo et al. 2021][Weigold et al. 2021]. Both, the quantum and the classical part, are required for the algorithm to work correctly. This also includes the control flow of the algorithm, which is included in the classical part of the algorithm.

Integrating a quantum algorithm into an application requires the implementation of the entire algorithm. A dedicated interface is required to enable the integration into applications. Deploying the algorithm to a hybrid runtime, which can execute both the quantum and the classical part of the algorithm, even requires both parts to be deployed together.

Solution

Package the entire quantum algorithm, i.e., both the quantum parts and the classical parts, as a Hybrid Module. This module can be composed of smaller modules, e.g., Quantum Modules. It also contains the control flow logic to orchestrate the quantum and classical computation. The Hybrid Module should provide an interface that facilitates its integration into applications. This interface should mainly accept the required problem-specific input values, i.e., the problem that should be processed by the algorithm. Moreover, the interface of a Hybrid Module can also allow behavior inputs to the classical as well as quantum computation, similar to the Quantum Module Templates.

Solution sketch hybrid module

An exemplary sketch of a Hybrid Module is shown in the sketch. It includes the control flow logic and implementations of classical and quantum parts with a loop between quantum and classical computation. The implementation of such a hybrid module can consist of multiple smaller modules, e.g., the three classical and one quantum computation steps shown can each be implemented in a separate module.

Result

The entire quantum algorithm implementation is packaged as a Hybrid Module. It contains both the quantum and the classical parts, as well as the control flow logic. Hybrid Modules can be used to deploy the algorithm as a standalone service, e.g., in a hybrid runtime environment that can execute both the classical and the quantum part [Riel 2022]. Furthermore, a Hybrid Module can be distributed as a library that implements the quantum algorithm and can be integrated into classical applications. It provides an interface for the application to use. To facilitate the integration of a Hybrid Module by problem-domain experts, a Classical-Quantum Interface can be used as the modules’ interface.

Related Patterns

The quantum part of the algorithm implementation inside a Hybrid Module can be organized into Quantum Modules and Quantum Module Templates. To facilitate their integration into applications by problem-domain experts without quantum computing knowledge, the Hybrid Module can expose a problem domain-specific Classical-Quantum Interface.

Known Uses

One concrete example are implementations of Shor’s algorithm [Shor 1997] which computes the prime factors of the input number. The period-finding calculated on the quantum computer and the classical post-processing performing the continued fraction expansion is packaged as a Hybrid Module in Amazon Braket, Qiskit and [Q#](https://learn.microsoft.com/en-us/azure/quantum/ overview-what-is-qsharp-and-qdk).

Other examples of Hybrid Modules are implementations of VQAs [Cerezo et al. 2021], e.g., QAOA and VQE implementations for the Qiskit Runtime contain the full quantum algorithm implementation. [Beisel et al. 2023] showcase a service ecosystem enabling a workflow-based composition of Hybrid Modules for VQAs.