Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for OpenQASM 3.0 looping and branching #33

Open
burgholzer opened this issue Dec 16, 2021 · 0 comments
Open

Support for OpenQASM 3.0 looping and branching #33

burgholzer opened this issue Dec 16, 2021 · 0 comments
Labels
Core Anything related to the Core library and IR enhancement New feature or request

Comments

@burgholzer
Copy link
Member

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).

@burgholzer burgholzer added the enhancement New feature or request label Dec 16, 2021
burgholzer pushed a commit that referenced this issue Jun 2, 2023
@burgholzer burgholzer added this to the OpenQASM 3.0 Support milestone Jun 15, 2023
@burgholzer burgholzer added the Core Anything related to the Core library and IR label Jun 15, 2023
burgholzer added a commit that referenced this issue Dec 13, 2023
This PR replaces the existing OpenQASM 2.0 parser with a new OpenQASM
3.0 parser.

The new parser now builds a syntax tree, where type checking, constant
evaluation, and translation to the Quantum circuit.

The parser can handle the following new features:

### New Syntax

New syntax for declaring bits, qubit, measure operations. The old syntax
(`creg`, `qreg`) is still supported.

```qasm
qubit[8] q;
bit[8] c;

c[0] = measure q[0];
measure q[1] -> c[1];

if (c[0] == 1) {
  x q[0];
}
```

### Gate modifiers

Gate modifiers (`inv`, `ctrl`, and `negctrl`) are now supported. This
replaces the `c` prefix.
See the OpenQASM 3.0 specification for more information:
https://openqasm.com/language/gates.html#quantum-gate-modifiers

```qasm
ctrl @ x q[0], q[1]; // Equivalent to cx q
ctrl(2) @ x q[0], q[1], q[2]; // Equivalent to ccx q;
```

### Classical constant values

The parser now supports classical computation with constant values. This
can be used to e.g. define the number of quantum registers.

```qasm
const uint N = 4;
qubit[N * 2];
x qubit[N * 2 - 1];
```

Additionally, all features of the previous parser are still supported.


The big features from OpenQASM 3.0 still missing are:

- classical computational features such as loops, functions, etc. (see
#33)
- types such as bools, floats, angles, complex types, etc. (see #30,
#32)
- `pow` modifier (#27)

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Lukas Burgholzer <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Core Anything related to the Core library and IR enhancement New feature or request
Projects
Status: Todo
Status: Todo
Development

No branches or pull requests

1 participant