-
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
Composite key for Primitives #8604
Conversation
Thank you for opening a new pull request. Before your PR can be merged it will first need to pass continuous integration tests and be reviewed. Sometimes the review process can be slow, so please be patient. While you're waiting, please feel free to review other open PRs. While only a subset of people are authorized to approve pull requests for merging, everyone is encouraged to review open pull requests. Doing reviews helps reduce the burden on the core team and helps make the project's code better for everyone. One or more of the the following people are requested to review this:
|
Pull Request Test Coverage Report for Build 3057558417
💛 - Coveralls |
It seems to me that you are trying to implement some sort of |
True, it's similar, but I don't want to have a hash in the mutable QuantumCircuit, and I believe there is collision for malicious cases in my proposal. Instead of providing hash as an official API, I would like to use it in this way for internal implementation. |
To fix #8725, let's merge this PR ASAP. |
It might be good to have more attributes. The following example generates the same value though the number of qubits and classical bits are different. from qiskit import QuantumCircuit
from qiskit.primitives.utils import _circuit_key
def foo(n):
qc = QuantumCircuit(n, n, name='foo')
return qc
for i in range(5):
print(_circuit_key(foo(i))) output
|
Another collision example from qiskit import QuantumCircuit
from qiskit.primitives.utils import _circuit_key
def foo(n):
qc = QuantumCircuit(1, 1, name='foo')
qc.ry(n, 0)
return qc
for i in range(5):
print(_circuit_key(foo(i))) output
|
We perhaps have to enumerate from qiskit import QuantumCircuit
from qiskit.primitives.utils import _circuit_key
def to_tuple(qc):
lst = []
for inst in qc.data:
op = inst.operation
lst.append(((op.name, op.num_qubits, op.num_clbits, tuple(op.params)), inst.qubits))
return tuple(lst)
def foo(n):
qc = QuantumCircuit(1, 1, name='foo')
qc.ry(n, 0)
tpl = to_tuple(qc)
print('foo', n)
print(tpl, hash(tpl))
return qc
for i in range(5):
print(_circuit_key(foo(i))) output:
|
pulse schedule is covered by |
We need some test cases of Sampler to cover #8725 and pulse schedule, for example, #8604 (comment). |
If we use |
It may also good take a plot of runtime of key generation (like #8604 (comment)) because it enumerates |
I updated the benchmark of my first comment. |
I also wonder whether In my humble opinion, this def __hash__(self):
key = _circuit_key(self)
return hash((self.name, self.metadata, key)) The main reason for this is to avoid re-computation (e.g. extra transpiling) for circuits which are functionally identical. Regardless of what we decide, this should be clarified explicitly in the docs (i.e. whether |
Is it possible to make a unit test of pulse schedule? |
I added a test, but is this what you want? I am not so familiar with pulse scheduling. |
Thanks. I'm not familiar with pulse scheduling, but the test looks good to me. I will approve it. I noticed a tricky case with custom gate. We can make a custom gate with the same name and can cheat this PR. from qiskit import QuantumCircuit
from qiskit.primitives.utils import _circuit_key
def foo(n):
circ = QuantumCircuit(1, name='custom')
circ.ry(n, 0)
inst = circ.to_instruction()
qc = QuantumCircuit(1, 1, name='foo')
qc.append(inst, [0])
print('foo', n)
print(qc)
print(qc.decompose())
return qc
for i in range(5):
key = _circuit_key(foo(i))
print(hash(key), key) output
|
As my first comment,
The assumption is that this PR is better than the existing one. If the assumption is that it is better than the current one, then it is wrong not to include the id. In offline discussion, this PR does not give a permanent solution nor a hash of QuantumCircuit. This PR should wait for @jyu00's review, but we merge this once since this is blocking the development of applications. It may be changed in the future, including with or without id. |
Sounds good. Thank you for the update. |
Summary
I opened this PR for discussion.
=> Conclusion
fixes #8725
Details and comments
Alternatives:
pickle is a bit faster than qpy but serialization time is unstable...