mqt-core
mqt-core copied to clipboard
Support for OpenQASM 3.0 looping and branching
The OpenQASM 3.0 standard provides the means to describe the flow of a program using if
statements, for
and while
loops.
This allows to more easily write static quantum algorithms that consist of iterations (such as Grover's algorithm or Phase Estimation) but also allows for the construction of sequential quantum circuits (such as Repeat Until Success schemes).
As a starting point, the creation of static algorithms should be supported using these constructs, i.e., programs should be supported that only contain constants and hence make it possible to unroll all loops at compile-time. An example of such a circuit is shown in https://arxiv.org/pdf/2104.14722.pdf
/*
* Iterative phase estimation
*/
OPENQASM 3;
include "stdgates.inc";
const n = 3; // number of iterations
const θ = 3*π/8; //phase angle on target qubit
qubit q; // phase estimation qubit
qubit r; // target qubit for the controlled-unitary gate
angle[n] c = 0; // phase estimation bits
// initialize
reset q;
reset r;
// prepare uniform superposition of eigenvectors of phase
h r;
// iterative phase estimation loop
for i in [1:n] { // implicitly cast val to int
reset q;
h q;
ctrl @ pow(2**i) @ phase(θ) q, r;
inv @ phase(c) q;
h q;
measure q -> c[0];
// newest measurement outcome is associated to a π/2 phase shift
// in the next iteration, so shift all bits of c left
c <<= 1;
}
// Now c contains the n-bit estimate of φ in the
// eigenvalue e^{i*φ} and qreg r is projected to an // approximate eigenstate of the phase gate.
In theory, this could be implemented independently from the other OpenQASM 3.0 features, but it probably makes sense to add support for constant values first (#31).