Closed liu-zixiong closed 1 year ago
Updating the docstrings would be good. We've gone back & forth through the implementation of the operators as to what types we actually return, obviously the docstrings weren't kept up-to-date.
Thanks for the update. I manually checked through the types of all the operators, and it seems that there are actually 4 types that are returned: dia_matrix, csc_matrix, csr_matrix, bsr_matrix.
Edited the docstrings, changes have been pushed.
Hello everyone! I recently noticed this issue with ParameterizedUnitaryGate() regarding numpy.ndarray type. Although the docstrings under each function in operators.py say that ndarray operator matrices are returned, in reality ParameterizedUnitaryGate() does not accept numpy.ndarray as a valid type, and that the actual type that is returned is scipy.sparse._csc.csc_matrix.
Let me illustrate with a tester function I wrote. Within operators.py, define a function that returns a different matrix type depending on the input given. The unitary condition for matrix input into circuits is met for cases 1 - 2 by sample matrix being unitary to begin with, and cases 3- 6 via the additional 1j factor before exponentiation.
When we inspect the type of the values returned, this function will output the following for its respective flag cases. 0) Python list 1) numpy.ndarray 2) scipy.sparse._csc.csc_matrix 3) numpy.ndarray 4) scipy.sparse._csc.csc_matrix 5) scipy.sparse._csc.csc_matrix 6) scipy.sparse._csc.csc_matrix
Now, define a function within circuit.py that allows us to feed an int arg into our typetester, using ParameterizedUnitaryGate. The code below is directly adapted from cv_d. The only things changed were the names of certain variables, and the label.
Now, we can write a circuit that uses our typetester.
When we progressively feed circuit.cv_typetest the values 0 - 6, we will find that for cases 0, 1 and 3, ParameterizedUnitaryGate will throw "UserWarning: Unable to define gate", before attempting to feed Qiskit the circuit regardless, and then Qiskit will throw a "QiskitError: "Cannot unroll the circuit to the given basis, ['ccx', 'cp', 'cswap', 'csx', 'cu'......]". The full traceback is included at the end of this post.
I suggest editing the docstring descriptions for all of the operators.py functions since it's clear that ParameterizedUnitaryGate isn't actually able to accept the numpy.ndarray type. Otherwise it may be misleading to other users.
Full Error Message for cases 0, 1, and 3.