Description
What's the problem this feature will solve?
It's been a while since Qiskit has introduced "Classical feedforward and control flow", which matches OpenQASM 3's "Looping and branching".
Within the MQT, support for these dynamic circuit primitives is still limited.
We support mid-circuit measurements, reset operations, as well as a limited set of classically-controlled operations, which roughly match the capabilities of OpenQASM 2's if
statement (see https://arxiv.org/abs/1707.03429v2). See also the MQT Core IR quickstart guide.
The goal of this issue is to add proper support for Qiskit's IfElseOp
to the MQT Core IR so that Qiskit circuits using such instructions can be imported into the MQT, used within it, as well as exported back to Qiskit.
This requires
- adapting the MQT Core IR to be capable of properly representing an
if (...) { ... } else { ... }
construct - adapting the Qiskit import and export routines to handle the newly-supported operation
- adapting the OpenQASM3 parser and exporter to handle the newly supported operation.
- refactoring any dependant code to use the new functionality.
Describe the solution you'd like
Any solution to this issue may take inspiration from the way Qiskit is handling these operations. Specifically, the ControlFlowOp
class as well as the IfElseOp
class.
It probably makes sense to replicate a similar operation hierarchy in MQT Core.
So instead of
Operation --> ClassicControlledOperation
it is probably reasonable to refactor this to
Operation --> ControlFlowOperation --> IfElseOperation
For the purpose of this PR, it is fine if the IfElseOperation
only supports conditions in the form they are currently supported throughout MQT Core, that is
- either a full classical register is compared to an integer, or
- a single bit is compared against a Boolean value.
Where to find the respective code?
The classically-controlled operations are currently defined here: https://github.com/munich-quantum-toolkit/core/blob/main/include/mqt-core/ir/operations/ClassicControlledOperation.hpp and contain the following members
core/include/mqt-core/ir/operations/ClassicControlledOperation.hpp
Lines 132 to 136 in d88d1f0
They are exposed as utility functions in the QuantumComputation
class
core/include/mqt-core/ir/QuantumComputation.hpp
Lines 336 to 362 in d88d1f0
In a similar fashion, they are exposed to Python
core/src/python/ir/register_quantum_computation.cpp
Lines 421 to 473 in d88d1f0
Neither the Qiskit import nor the export handles that type of gate yet:
core/src/mqt/core/plugins/qiskit/qiskit_to_mqt.py
Lines 113 to 171 in d88d1f0
core/src/mqt/core/plugins/qiskit/mqt_to_qiskit.py
Lines 267 to 269 in d88d1f0
The OpenQASM 3 importer breaks down if-else statements into two separate ClassicControlledOperation
s:
Lines 892 to 929 in d88d1f0
The corresponding exporter works for the limited scope of the
ClassicControlledOperation
: core/src/ir/operations/ClassicControlledOperation.cpp
Lines 130 to 153 in d88d1f0
Further places where the ClassicControlledOperation class is used can easily be found via code search.