Qiskit-terra: QuantumCircuit constructor fails if n_qubits is np.int64

Created on 7 Feb 2020  路  4Comments  路  Source: Qiskit/qiskit-terra


What is the expected enhancement?

Python 3.5.6 |Anaconda, Inc.| (default, Aug 26 2018, 16:30:03)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> import qiskit as qk
>>> n = np.int64(12)
>>> qr = qk.QuantumRegister(n)
>>> qc = qk.QuantumCircuit(n)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/kevin.krsulichibm.com/q/qiskit-terra/qiskit/circuit/quantumcircuit.py", line 157, in __init__
    self.add_register(*regs)
  File "/Users/kevin.krsulichibm.com/q/qiskit-terra/qiskit/circuit/quantumcircuit.py", line 541, in add_register
    if register.name in [reg.name for reg in self.qregs + self.cregs]:
AttributeError: 'numpy.int64' object has no attribute 'name'

Similar to as was added for Registers in #2288, the QuantumCircuit constructor should test if a provided argument can be cast to an int before it raises an error.

For reference, here is where Register does the check:
https://github.com/Qiskit/qiskit-terra/blob/2ee7a3a/qiskit/circuit/register.py#L39

and where QuantumCircuit makes new Registers from the provided int:
https://github.com/Qiskit/qiskit-terra/blob/703c9a3/qiskit/circuit/quantumcircuit.py#L522

bug good first issue help wanted low pending PR enhancement

All 4 comments

@1ucian0 have you made progress on this? I'd like to give it a go if not (I'm new to qiskit and open source and this looks like a nice first issue to try out!)

Sure! Go ahead!

Yay great I'll get cracking! Is there a corresponding test file that needs updating as well?

Yes, tests should be added (maybe in the files we already have?). You can use the code from OP for that.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

derivation picture derivation  路  5Comments

indisoluble picture indisoluble  路  3Comments

ajavadia picture ajavadia  路  5Comments

ajavadia picture ajavadia  路  7Comments

1ucian0 picture 1ucian0  路  6Comments