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

zx.qasm does not recognize gates imported from qelib1.inc #48

Closed
mhlr opened this issue Jul 17, 2020 · 5 comments
Closed

zx.qasm does not recognize gates imported from qelib1.inc #48

mhlr opened this issue Jul 17, 2020 · 5 comments

Comments

@mhlr
Copy link

mhlr commented Jul 17, 2020

qelib1.inc defines the controlled Hadamard gate ch, but zx.qasm does not recognize ch even when qelib1.inc is imported

import pyzx
g = zx.qasm('''
OPENQASM 2.0;
include "qelib1.inc";

qreg a[2];
ch a[0], a[1];
''')

TypeError Traceback (most recent call last)
in
6 qreg a[2];
7 ch a[0], a[1];
----> 8 ''')

/opt/conda/lib/python3.7/site-packages/pyzx/circuit/qasmparser.py in qasm(s)
188 """Parses a string representing a program in QASM, and outputs a Circuit."""
189 p = QASMParser()
--> 190 return p.parse(s, strict=False)
191

/opt/conda/lib/python3.7/site-packages/pyzx/circuit/qasmparser.py in parse(self, s, strict)
64 gates: List[Gate] = []
65 for c in commands:
---> 66 self.gates.extend(self.parse_command(c, self.registers))
67
68 circ = Circuit(self.qubit_count)

/opt/conda/lib/python3.7/site-packages/pyzx/circuit/qasmparser.py in parse_command(self, c, registers)
182 gates.append(g)
183 continue
--> 184 raise TypeError("Unknown gate name: {}".format(c))
185 return gates
186

TypeError: Unknown gate name: ch a[0], a[1]

@mhlr mhlr changed the title zx.qasm does not recognize gates from qelib1.inc zx.qasm does not recognize gates imported from qelib1.inc Jul 17, 2020
@jvdwetering
Copy link
Collaborator

Okay, I can add a controlled-Hadamard gate to PyZX. Are there any other gates from the standard QASM library that are missing?

@mhlr
Copy link
Author

mhlr commented Aug 6, 2020

Controlled Hadamard is the only one I tried. I am new to both ZX and QASM. PyZX does not read qelib1.inc to get the definitions?

@gwwatkin
Copy link

Hi @jvdwetering , it looks like the crz gates are also missing:

[...]
    pyzx_circ = zx.Circuit.load(fname)
  File "/home/george/.local/lib/python3.9/site-packages/pyzx/circuit/__init__.py", line 276, in load
    return Circuit.from_qasm_file(circuitfile)
  File "/home/george/.local/lib/python3.9/site-packages/pyzx/circuit/__init__.py", line 360, in from_qasm_file
    c = p.parse(f.read())
  File "/home/george/.local/lib/python3.9/site-packages/pyzx/circuit/qasmparser.py", line 66, in parse
    self.gates.extend(self.parse_command(c, self.registers))
  File "/home/george/.local/lib/python3.9/site-packages/pyzx/circuit/qasmparser.py", line 206, in parse_command
    raise TypeError("Unknown gate name: {}".format(c))
TypeError: Unknown gate name: crz(pi/2) q[1],q[0]

It would be very helpful to our project.

@jvdwetering
Copy link
Collaborator

So, for the CRZ gate there is a bit of a difficulty in which phase convention to adopt. The qiskit library (https://qiskit.org/documentation/stubs/qiskit.circuit.library.CRZGate.html) apparently sees an RZ(alpha) gate as diag(e^{-i alpha/2},e^{i alpha/2}). This leads to a different controlled phase gate than I would expect (namely, a matrix with 2 diagonal elements not equal to 1, instead of just 1 element not equal to 1). For instance, a CRZ(pi) gate does not match the CZ gate.
This does seem to be the way it is defined in the QASM spec however (https://arxiv.org/pdf/1707.03429.pdf, p.12), so I guess I'll just follow that.

@dlyongemallo
Copy link
Contributor

In case anyone comes across this issue later, support for all the gates mentioned above have all been added as of PR #156.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants