-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Cannot create a numpy array of Registers #1898
Comments
Just out of curiosity, why do you want to put QuantumRegisters in a numpy array? |
I was writing code to automate management of ancillary qubits and I was using numpy arrays to store the |
I expect that quantum software components will be integrated into classical data structures more and more as we move forward so we should ensure that these operate as a programmer would expect them to when that happens. |
I agree this is an issue (not an easy one, so removing It seems that @Woody1193 would expect this: class QuantumRegisterDT(QuantumRegister):
__len__ = property(lambda _: AttributeError)
x = np.array([], dtype = object, ndmin = 1)
x = np.append(x, [QuantumRegisterDT(1) for _ in range(5)])
len(x) -> 5
x -> [QuantumRegisterDT(1, 'q0') QuantumRegisterDT(1, 'q1') QuantumRegisterDT(1, 'q2') QuantumRegisterDT(1, 'q3') QuantumRegisterDT(1, 'q4')] Nevertheless, I can argue that is also not correct. class Bit():
def __init__(self, register, index):
self.register = register
self.index = index
def __repr__(self):
return "(%s,%s)" % (self.register, self.index)
class ExtendedQuantumRegister(QuantumRegister):
def __iter__(self):
"""
Returns:
iterator: an iterator over the bits/qubits of the register, in the
form `tuple (Register, int)`.
"""
for qubit in zip([self]*self.size, range(self.size)):
yield Bit(qubit[0],qubit[1])
x = np.array([], dtype = object, ndmin = 1)
x = np.append(x, [ExtendedQuantumRegister(1) for _ in range(5)])
print(len(x))
print(x) 5
[(ExtendedQuantumRegister(1, 'q0'), 0)
(ExtendedQuantumRegister(1, 'q1'), 0)
(ExtendedQuantumRegister(1, 'q2'), 0)
(ExtendedQuantumRegister(1, 'q3'), 0)
(ExtendedQuantumRegister(1, 'q4'), 0)] In this way, np.append(x, [ExtendedQuantumRegister(2) for _ in range(5)]) [(ExtendedQuantumRegister(2, 'q0'),0) (ExtendedQuantumRegister(2, 'q0'),1)
(ExtendedQuantumRegister(2, 'q1'),0) (ExtendedQuantumRegister(2, 'q1'),1)
(ExtendedQuantumRegister(2, 'q2'),0) (ExtendedQuantumRegister(2, 'q2'),1)
(ExtendedQuantumRegister(2, 'q3'),0) (ExtendedQuantumRegister(2, 'q3'),1)
(ExtendedQuantumRegister(2, 'q4'),0) (ExtendedQuantumRegister(2, 'q4'),1)] So, go ahead and convince me that the I'm wrong :) @ajavadia @Woody1193 |
CC @nonhermitian , who usually have good insights on what a NumPy user would expect. |
I would expect an array of registers to contain registers in the same manner as a list. The original posters code however is confusing because you cannot have register as a dtype, it would be object, and doing append is super inefficient. However, our objects should play nice with numpy arrays because it is a fundamental scientific python library. |
I'm not fully sure if I understand. You mean this?
|
@1ucian0, the specific issue I'm having is that when you index into a
I can get around this by doing |
Yes. We agree that returning a tuple is a bad idea. However, if we want to keep
I'm just trying to make sure we are all on the same page on what should it be the result. |
With PR #2414: from qiskit import QuantumRegister
import numpy as np
x = np.array([], dtype = object, ndmin = 1)
x = np.append(x, [QuantumRegister(2) for _ in range(5)])
print(len(x))
print(x) 10
[QuBit(QuantumRegister(2, 'q0'), 0) QuBit(QuantumRegister(2, 'q0'), 1)
QuBit(QuantumRegister(2, 'q1'), 0) QuBit(QuantumRegister(2, 'q1'), 1)
QuBit(QuantumRegister(2, 'q2'), 0) QuBit(QuantumRegister(2, 'q2'), 1)
QuBit(QuantumRegister(2, 'q3'), 0) QuBit(QuantumRegister(2, 'q3'), 1)
QuBit(QuantumRegister(2, 'q4'), 0) QuBit(QuantumRegister(2, 'q4'), 1)] |
Information
Qiskit Terra version: 0.7.1
Python version: 3.5.6 (Anaconda)
Operating System: Windows 7
What is the current behavior?
Attempting to call
append
orconcatenate
with a list ofRegister
objects results in a NumPy array that contains integer values along with theRegister
objects.Steps to reproduce the problem
What is the expected behavior?
This should first print 5 and then print an array with 5 elements, all QuantumRegisters.
Suggested solutions
This is occurring because NumPy calls
__len__
when doing anappend
orconcatenate
operation. For the time being, I can dox[::2]
to remove the integer values but this is a stop-gap approach.The text was updated successfully, but these errors were encountered: