-
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
Improve performance of barrier() #11345
Conversation
Pull Request Test Coverage Report for Build 7530882971Warning: This coverage report may be inaccurate.We've detected an issue with your CI configuration that might affect the accuracy of this pull request's coverage report.
💛 - Coveralls |
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.
At the moment, this has similar problems surrounding the possibility of control flow as #10949 originally had. I think what you're after looks something a bit more like this (warning: untested):
qubits = tuple({q: None for ...} if qargs else self.qubits)
return self._current_scope().append(CircuitInstruction(Barrier(len(qubits)), qubits, ())
That correctly handles control-flow using the new scope API - self._current_scope()
returns an implementer of the internal CircuitScopeInterface
that abstracts the _append
operation (amongst others).
(btw: you don't need to press "update branch"; we automatically bring branches up-to-date when they join the merge queue)
@jakelishman Thanks for the pointer. How could I write a test for the control flow case? And what to test for exactly? I tried this, and it runs without errors.
|
Right - from qiskit import QuantumCircuit
qc = QuantumCircuit(2, 2)
qc.h(0)
qc.cx(0, 1)
qc.measure([0, 1], [0, 1])
with qc.if_test((qc.clbits[0], False)):
qc.h(0)
qc.barrier()
qc.h(0)
qc.barrier()
qc.h(0)
qc.draw()
where the barriers have migrated outside the "if". The result should look like if I'd manually constructed all the scopes explicitly: from qiskit import QuantumCircuit
qc = QuantumCircuit(2, 2)
qc.h(0)
qc.cx(0, 1)
qc.measure([0, 1], [0, 1])
true_body = QuantumCircuit(qc.qubits, [qc.clbits[0]])
true_body.h(0)
true_body.barrier()
true_body.h(0)
true_body.barrier()
true_body.h(0)
qc.if_test((qc.clbits[0], False), true_body, qc.qubits, [qc.clbits[0]])
qc.draw()
|
35ec9ee
to
e2c6c83
Compare
Updated benchmark:
Output of
|
qubits = tuple({q: None for qarg in qargs for q in self.qbit_argument_conversion(qarg)}) | ||
return self.append(CircuitInstruction(Barrier(len(qubits), label=label), qubits, ())) | ||
else: | ||
qubits = self.qubits.copy() |
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.
The copy
might not be necessary, but it does not hurt performance so I left it as it was originally coded.
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.
It's fine either way - self.qubits
is a semi-dangerous object because it's a direct view onto QuantumCircuit
internals and so mustn't be mutated, but the construction of a CircuitInstruction
will consume it into a tuple
anyway, so it wouldn't matter. Since it's irrelevant to performance, it's fine to just leave it.
qubits = tuple({q: None for qarg in qargs for q in self.qbit_argument_conversion(qarg)}) | ||
return self.append(CircuitInstruction(Barrier(len(qubits), label=label), qubits, ())) | ||
else: | ||
qubits = self.qubits.copy() |
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.
It's fine either way - self.qubits
is a semi-dangerous object because it's a direct view onto QuantumCircuit
internals and so mustn't be mutated, but the construction of a CircuitInstruction
will consume it into a tuple
anyway, so it wouldn't matter. Since it's irrelevant to performance, it's fine to just leave it.
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.
Thanks for sticking with this!
* Improve performance of barrier() * black * fix barrier statement in context * add test * black * fix label argument * Use `unittest` test method --------- Co-authored-by: Jake Lishman <[email protected]>
Summary
Improve performance of
barrier()
by using the_append
when possible.Details and comments
Benchmark:
Results:
This is an alternative to #10949 @jakelishman