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

Initilize instruction does not write a QASM definition #6169

Open
omarcostahamido opened this issue Apr 6, 2021 · 9 comments
Open

Initilize instruction does not write a QASM definition #6169

omarcostahamido opened this issue Apr 6, 2021 · 9 comments
Labels
bug Something isn't working mod: qasm2 Relating to OpenQASM 2 import or export

Comments

@omarcostahamido
Copy link
Contributor

Information

when building a quantum circuit from qasm: if the qasm code includes initialize it gets ignored (or throws an error).

  • Qiskit Terra version:
    Qiskit | 0.24.1
    -- | --
    Terra | 0.16.4
    Aer | 0.7.6
    Ignis | 0.5.2
    Aqua | 0.8.2
    IBM Q Provider | 0.12.2
  • Python version:
    Python | 3.8.8

Steps to reproduce the problem

from qiskit import QuantumCircuit
qc = QuantumCircuit(2)
qc.initialize([0,1,0,0],range(2))
qc.qasm()
qc2 = QuantumCircuit().from_qasm_str(qc.qasm())
@omarcostahamido omarcostahamido added the bug Something isn't working label Apr 6, 2021
@quantumjim
Copy link
Member

A point to note about this is that the from_qasm_str() method does not accept a qasm string created by the qasm() method.

@1ucian0
Copy link
Member

1ucian0 commented Apr 17, 2021

Is initialize supported by OpenQASM2?

@omarcostahamido
Copy link
Contributor Author

omarcostahamido commented Apr 17, 2021 via email

@1ucian0
Copy link
Member

1ucian0 commented Apr 17, 2021

I mean, they could as opaque instructions (one per each size of idlist)

OPENQASM 2.0;
include "qelib1.inc";

opaque initialize4_2(x0, x1, x2, x3) q0, q1;

qreg q[2];
initialize4_2(0,1,0,0) q[0],q[1];

However this wont make it loadable per se (an opaque instruction wont have a definition). Am I missing something?

@kdk kdk changed the title qasm's initialize Initilize instruction does not write a QASM definition Apr 21, 2021
@kdk kdk added the mod: qasm2 Relating to OpenQASM 2 import or export label Apr 21, 2021
@jwoehr
Copy link
Contributor

jwoehr commented Jul 3, 2021

I think the big problem with supporting initialize in OpenQASM 2 is that QuantumCircuit.initialize() can take a vector of complex amplitudes as in the example

circuit.initialize([1/np.sqrt(2), -1/np.sqrt(2)], 0)

in the Qiskit Terra API Documentation. This sort of thing cannot easily be represented within the limited parameter expression syntax of OpenQASM 2.0

@omarcostahamido
Copy link
Contributor Author

Thank you @jwoehr and @1ucian0 .
I understand qasm is really simple and won't understand np or that it is possibly more work to allow it to understand a vector of complex numbers. But something is better than nothing at all...
In my work to rebuild the qsphere visualization using javascript and opengl inside Max, I just added a feature to my microqiskit object to pass the statevector as a simple list of numbers. Assuming that all states are complex numbers, and that all complex numbers are just a group of 2 values (for real and imaginary components), it is simply waiting to receive a list of ((2^nqubits)*2) values.
Thoughts?

@jwoehr
Copy link
Contributor

jwoehr commented Jul 4, 2021

Well, I've got a working OpenQASM 2 translator based on the ANTLR4 grammar for OpenQASM 3. It's pretty easy to extend, but I'm not sure how much I can get away with on the current PR..

Currently I'm working on the test suite.

The next thing I was thinking to add is parameterization of translation so .qasm files can be re-used.

Somewhere along the line I should be able to add initialize.

@omarcostahamido
Copy link
Contributor Author

@jwoehr what would I do without you?
this sounds promising. And yes, I'm all in to become early adopter of OpenQASM 3; we should all move that direction sooner or later. Keep me posted please :)

@jwoehr
Copy link
Contributor

jwoehr commented Jul 4, 2021

We'll have to see if this PR flies. OpenQASM 3 is a profoundly more complex environment; I'm just using a grammar file authored by the Qiskit team on the basis of OpenQASM 3: A broader and deeper quantum assembly language. As the language evolves I may have to subset that grammar to keep this OpenQASM 2 development manageable. However, it's possible that instead we will move towards an "OpenQASM 2.1, 2.2 ..." which will migrate in stages towards OpenQASM 3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working mod: qasm2 Relating to OpenQASM 2 import or export
Projects
None yet
Development

No branches or pull requests

5 participants