-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Support "direct" Instruction -> SparsePauliOp
conversion
#11892
Comments
I think a All existing |
Actually, having just written that, two more thoughts:
|
Thanks for the input! I will do some more reading up on those existing paths. |
In what situations are you expecting to have a parametric |
I can see a use-case where one might want to convert between a circuit operation (i.e. from qiskit import QuantumCircuit
from qiskit.circuit import Parameter
from qiskit.quantum_info import Operator, SparsePauliOp
qc = QuantumCircuit(1)
qc.rx(1.57, 0)
rx_instruction = qc[0].operation
print(rx_instruction)
sop = SparsePauliOp.from_operator(Operator(rx_instruction))
print(sop)
a = Parameter("a")
qc_p = QuantumCircuit(1)
qc_p.rx(1.57 * a, 0)
rx_instruction_p = qc_p[0].operation
print(rx_instruction_p)
print(a * sop) This outputs:
Trying Traceback (most recent call last):
File "/home/oss/Files/Dev/Qiskit/qiskit/main/tmp-sparse-pauli-gate.py", line 21, in <module>
sop_p = SparsePauliOp.from_operator(Operator(rx_instruction_p))
File "/home/oss/Files/Dev/Qiskit/qiskit/main/qiskit/quantum_info/operators/operator.py", line 97, in __init__
self._data = self._init_instruction(data).data
File "/home/oss/Files/Dev/Qiskit/qiskit/main/qiskit/quantum_info/operators/operator.py", line 700, in _init_instruction
return Operator(np.array(instruction, dtype=complex))
File "/home/oss/Files/Dev/Qiskit/qiskit/main/qiskit/circuit/library/standard_gates/rx.py", line 125, in __array__
cos = math.cos(self.params[0] / 2)
File "/home/oss/Files/Dev/Qiskit/qiskit/main/qiskit/circuit/parameterexpression.py", line 415, in __float__
raise TypeError(
TypeError: ParameterExpression with unbound parameters (dict_keys([Parameter(a)])) cannot be cast to a float. I am aware that my proposal has quite some caveats and I also understand that a user may be facing a possibly exponential blow-up of parameter expressions when doing the above repeatedly and composing the results. Nonetheless, I think that a "symbolic" interpretation of a gate in terms of Pauli terms can have value. |
What should we add?
I have recently found myself having to convert an
Instruction
object into aSparsePauliOp
.This is currently possible via:
While this works, internally this will convert the instruction to a matrix and subsequently decompose that Matrix into Pauli terms. Another limitation is that this does not work for parameterized instructions.
Thus, I would like to propose a new method be added to the gate objects (not sure which exact class this would end up on). This method should:
As an initial naming suggestion I propose
to_symbolic
(or something along these lines), to indicate that this can handle parameters. At the same time this also hints at the "symbolic" form in terms of Paulis.The example at the top could be used as a fallback implementation which would gracefully handle encountering a parameter.
For this method to be truly useful, we might need #11891 in order to retain the gate qubit indices as part of the
SparsePauliOp
. But I think an initial implementation can already be done without this logic.I am happy to contribute a PR for this. I would require minimal guidance on the interplay of the different gate classes (e.g.
Gate
,Instruction
, etc.).The text was updated successfully, but these errors were encountered: