-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Adding Pauli to the standard gate library #5153
Conversation
""" | ||
import numpy as np | ||
|
||
from qiskit import QuantumCircuit, QuantumRegister |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
from qiskit import QuantumCircuit, QuantumRegister | |
from qiskit.circuit.quantumcircuit import QuantumCircuit | |
from qiskit.circuit.quantumregister import QuantumRegister |
This should solve the circular import errors
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks mostly good. I have a couple of requests for renaming variables to match quantum_info operators. It also looks like the input string is reversed from the label parameter of the Operator
and Pauli
classes.
Another thing I would add is to change the Pauli.to_instruction
method to return one of these gate objects rather than a circuit containing Pauli gates:
class Pauli:
...
def to_instruction(self):
from qiskit.circuit.libarary.standard_gates import PauliGate
return PauliGate(str(self))
|
||
def __init__(self, pauli_string="I"): | ||
if isinstance(pauli_string, float): | ||
pauli_string = "I" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this raise an exception if its not a string?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is certainly a welcome behavior, but it clashes with many automatic tests which implicitly assume the parameters a gate can have consist of a vector of floats (and sometimes just assign random parameters based on this assumption). For now it's either that or modification of the testing framework.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kdk Is the testing framework easy to modify for this? Seems not ideal to have to add this to the class just for the automatic tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, this should be easy to fix. Which tests are failing? There are tests to keep some expectations on standard gates (e.g. that their gate parameters align with their definition parameters, ...) but they each already have cases handling gates that have non-standard signatures. In fact, it looks like those have already been updated to handle PauliGate
s.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added workaround in the test files; you might have a better idea how to circumvent this.
The most problematic case is what we have in test_to_matrix
and test_to_matrix_op
: here the params are initialized once params = [0.1 * i for i in range(1, 11)]
, before being applied to each gate; the only difference is the computed number of actual parameters used from this list.
Since PauliGate
expects str
and not float
, this crashes without direct override (as I added).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we are storing the label in the params, we don't really need to store it also as an attribute, we can just get it from self.parameters[0]
in other functions that use it. Alternatively we don't store parameters at all and modify the assemble
method of this class to use the attribute rather than params.
ie.
def assemble(self):
instruction = super().assemble()
instruction.params = [self.pauli_string]
return instruction
This later way might be preferable if we want to move towards having all gate parameters be floads (@ajavadia what do you think?)
For me, Also, looking now, this would be better for |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, the only thing missing is a release note.
Summary
A
PauliGate
which applies pauli operations (given as a string) to a set of qubits is added to the standard library.Details and comments
While functionally equivalent to sequential application of pauli gates, this instruction can be performed efficiently by statevector simulator using a single pass on the statevector.