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 #116

Closed
fvirdia opened this issue Jun 1, 2023 · 4 comments
Closed

Support for OpenQASM 3.0 #116

fvirdia opened this issue Jun 1, 2023 · 4 comments
Labels
bug Something isn't working enhancement New feature or request

Comments

@fvirdia
Copy link

fvirdia commented Jun 1, 2023

Hi there,

I am trying to use PyZX to attempt reducing the footprint of some circuits I designed in Qiskit.
When trying to run pyzx.qasm on the QASM source generated by Qiskit, I was getting an error on import 'stdgates.inc', which I understand is a library of "standard" gates used by Qiskit.

I tried replacing the line with the content of stdgates.inc, however this results in TypeError: Arguments for custom gates are currently not supported: p(λ) a { ctrl @ gphase(λ) a; } on the very first "phase" gate in the file.

I could try downgrading to a previous Qiskit, however I was wondering if there is any way to support this out of the box, since anyway it seems OpenQASM 2.0 is considered deprecated.

@jvdwetering
Copy link
Collaborator

I think this particular issue can probably be quite easily fixed with a hacky solution. Namely just acknowledging that import 'stdgates.inc' is a standard thing to do in QASM3.0, and realizing that p(alpha) is a synonym for doing a Z rotation. Do you think that this would be enough to solve your issue, or are you also using other features from Qasm3.0 that currently aren't supported?

@jvdwetering jvdwetering added bug Something isn't working enhancement New feature or request labels Jun 7, 2023
dlyongemallo added a commit to dlyongemallo/pyzx that referenced this issue Sep 5, 2023
… in pyzx.

The OpenQASM 2 spec defines the following:
```
gate rz(phi) a { u1(phi) a; }
gate crz(lambda) a,b
{
    u1(lambda/2) b;
    cx a,b;
    u1(-lambda/2) b;
    cx a,b;
}
gate cu1(lambda) a,b
{
    u1(lambda/2) a;
    cx a,b;
    u1(-lambda/2) b;
    cx a,b;
    u1(lambda/2) b;
}
```

pyzx's `ZPhase` gate and `CRZ` gates are equivalent to, respectively, OpenQASM 2's `rz` (a synonyn for `u1`) and `crz` gates. Confusingly, the `crz` gate is not simply a controlled version of the `rz` gate; that gate is named `cu1`.

Meanwhile, the OpenQASM 3 spec defines:
```
gate rz(λ) a { gphase(-λ/2); U(0, 0, λ) a; }
gate crz(θ) a, b { ctrl @ rz(θ) a, b; }
```

This has the advantage that `crz` is now simply a controlled version of `rz`, at the expense of changing the meaning of `rz` so that it no longer matches `u1`/`ZPhase`.

This difference in conventions leads to user confusion and coding errors, as seen in issues zxcalc#102 and zxcalc#103. As well, changing the behaviour of `qasmparser` depending on the version of OpenQASM used is a blocker for issue zxcalc#116.
dlyongemallo added a commit to dlyongemallo/pyzx that referenced this issue Sep 5, 2023
… in pyzx.

The OpenQASM 2 spec defines the following:
```
gate rz(phi) a { u1(phi) a; }
gate crz(lambda) a,b
{
    u1(lambda/2) b;
    cx a,b;
    u1(-lambda/2) b;
    cx a,b;
}
gate cu1(lambda) a,b
{
    u1(lambda/2) a;
    cx a,b;
    u1(-lambda/2) b;
    cx a,b;
    u1(lambda/2) b;
}
```

pyzx's `ZPhase` gate and `CRZ` gates are equivalent to, respectively, OpenQASM 2's `rz` (a synonym for `u1`) and `crz` gates. Confusingly, the `crz` gate is not simply a controlled version of the `rz` gate; that gate is named `cu1`.

Meanwhile, the OpenQASM 3 spec defines:
```
gate rz(λ) a { gphase(-λ/2); U(0, 0, λ) a; }
gate crz(θ) a, b { ctrl @ rz(θ) a, b; }
```

This has the advantage that `crz` is now simply a controlled version of `rz`, at the expense of changing the meaning of `rz` so that it no longer matches `u1`/`ZPhase`.

This difference in conventions leads to user confusion and coding errors, as seen in issues zxcalc#102 and zxcalc#103. As well, changing the behaviour of `qasmparser` depending on the version of OpenQASM used is a blocker for issue zxcalc#116.
dlyongemallo added a commit to dlyongemallo/pyzx that referenced this issue Sep 7, 2023
Addresses issue zxcalc#116:
- accept OpenQASM 3 file header produced by qiskit
- condition behaviour of `rz` based on version:
  - OpenQASM 2: same as `ZPhase` = `diag(1, exp(i*phi))` (unchanged)
  - OpenQASM 3: acts like `diag(exp(-i*phi/2), exp(i*phi/2))`
- add `p` as an alias for `ZPhase` in OpenQASM 3
- add `u1` as an alias for `ZPhase` in both versions
@dlyongemallo
Copy link
Contributor

Hi, @fvirdia,

Is there anything else you need? PR #151 was just merged which adds support for the OpenQASM 3 header format and the p gate.

@dlyongemallo
Copy link
Contributor

PR #156 adds supports for OpenQASM 3 format. I tried some exports from qiskit and they seem to import just fine, so unless there are requests for additional qasm gates not covered by that PR, this issue can be closed.

@dlyongemallo
Copy link
Contributor

I think this issue can be closed now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants