Skip to content
New issue

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

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

Already on GitHub? # to your account

qiskit.qasm3.dumps raise TypeError: only length-1 arrays can be converted to Python scalars #13362

Closed
harui2019 opened this issue Oct 23, 2024 · 4 comments · Fixed by #13633
Closed
Labels
bug Something isn't working

Comments

@harui2019
Copy link

Environment

  • Qiskit version:
qiskit                    1.2.4
qiskit-aer                0.15.0
qiskit-aer-gpu            0.14.2
qiskit-algorithms         0.3.0
qiskit-ibm-provider       0.11.0
qiskit-ibm-runtime        0.30.0
qiskit-qasm3-import       0.5.0
  • Python version:
Python 3.12.2 | packaged by conda-forge | (main, Feb 16 2024, 20:50:58) [GCC 12.3.0] on linux
  • Operating system:
Ubuntu 24.04.1 LTS x86_64

What is happening?

A numpy type error raised during the execution of qiskit.qasm3.dumps .

Image
Image

How can we reproduce the issue?

from qiskit import QuantumCircuit
from qiskit.quantum_info import random_unitary
from qiskit.qasm3 import dumps

qc = QuantumCircuit(4)
for i in range(4):
    qc.h(i)
    qc.append(random_unitary(2),[i])
    
print(qc.draw())

dumps(qc)

or

from qiskit import QuantumCircuit
from qiskit.quantum_info import random_unitary
from qiskit.qasm3 import dumps

qc = QuantumCircuit(4)
for i in range(4):
    qc.h(i)
    qc.append(random_unitary(2).to_instruction(),[i])
    
print(qc.draw())

dumps(qc)

What should happen?

I should be able to dump this circuit to QASM3 because qiskit.qasm2.dumps can successfully dump this circuit.

Any suggestions?

This issue appears to be mentioned in #12013 (comment)

@harui2019 harui2019 added the bug Something isn't working label Oct 23, 2024
@ldi18
Copy link

ldi18 commented Nov 11, 2024

Hi, I just reproduced the error and would like to work on this as my first contribution to Qiskit.

@gadial
Copy link
Contributor

gadial commented Nov 11, 2024

@ldi18 feel free to go ahead. As far as I can see, this is a problem with QASM3 operating on the params of the unitary gates (which amount to its matrix) as opposed to working on the params of its definition circuit (as QASM2 does).

@ldi18
Copy link

ldi18 commented Nov 12, 2024

@gadial When I move the complex-to-string conversion into a function and apply it to each matrix element,

    def complex_to_str(inpt):
        """Convert a complex number to a string with formatting."""
        complex_inpt = complex(inpt)
        real, imag = map(normalize, [complex_inpt.real, complex_inpt.imag])

        jstr = "\\jmath" if output == "latex" else "j"
        if real == "0" and imag != "0":
            str_out = imag + jstr
        elif real != "0" and imag != "0":
            op_str = "+"
            # Remove + if imag negative except for latex fractions
            if complex_inpt.imag < 0 and (output != "latex" or "\\frac" not in imag):
                op_str = ""
            str_out = f"{real}{op_str}{imag}{jstr}"
        else:
            str_out = real
        return str_out

    if isinstance(inpt, np.ndarray):
        return np.reshape([complex_to_str(val) for val in inpt.flatten()], inpt.shape)
    return complex_to_str(inpt)

I get the following qasm3 string:

('OPENQASM 3.0;\n'
 'include "stdgates.inc";\n'
 'gate unitary(_gate_p_0) _gate_q_0 {\n'
 '  U(1.2181930078303413, 3.1803949087168197, 0.3822577065102588) _gate_q_0;\n'
 '}\n'
 'qubit[2] q;\n'
 "unitary([['-0.6309366264965464-0.5240133949751805j'\n"
 "  '0.27200760222784093+0.5033297125761635j']\n"
 " ['0.4256140835502845+0.3823370072532796j'\n"
 "  '0.36164925477003157+0.7361255879892625j']]) q[0];\n")

When importing the string with qasm3.loads(s), it gives the error line 7:9 no viable alternative at input 'unitary(['. Is this due to a missing unitary function in the loads() method? Or should the unitary([[..., ...],[..., ...]]) rather be a U(1.2181930078303413, 3.1803949087168197, 0.3822577065102588) as in the old qasm format?

@gadial
Copy link
Contributor

gadial commented Nov 12, 2024

I don't think you should handle the matrix elements. In QASM2 the matrix params do not appear on the output; it adds the definition of the gate (i.e. a circuit composed of basic gates which implements it), and I guess QASM3 should do this as well.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants