Skip to content
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 RB circuit generation #1317

Merged
merged 4 commits into from
Nov 28, 2023

Conversation

eendebakpt
Copy link
Contributor

@eendebakpt eendebakpt commented Nov 14, 2023

Summary

In this PR we improve performance of the RB circuit generation.

Details and comments

The main performance bottleneck is the repeat application of append of sequence elements to a circuit. We can improve performance by using the _append for the Barrier gate.

For the appending of the Clifford gates we could also use _append (in particular if the sequence elements are integral, in which case the internal generation using _to_instruction guarantees the elements to not need checking), but since the StandardRB is subclassed we have no full information about the elements in the sequence and have chosen not to do this.

Profiling on main:

image

PR:

image

PR checklist (delete when all criteria are met)

  • I have read the contributing guide CONTRIBUTING.md.
  • I have added the tests to cover my changes.
  • I have updated the documentation accordingly.
  • I have added a release note file using reno if this change needs to be documented in the release notes. Internal change, no release notes needed

Copy link
Collaborator

@itoko itoko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @eendebakpt . This is a simple but good improvement I'd like to have. The use of QuantumCircuit._append now sounds good to me since I've heard it's a semi-public API, which should be stable. I just suggest a minor improvement for the correct use of the API.

@@ -291,7 +291,7 @@ def _sequences_to_circuits(
circ = QuantumCircuit(self.num_qubits)
for elem in seq:
circ.append(self._to_instruction(elem, basis_gates), circ.qubits)
circ.append(Barrier(self.num_qubits), circ.qubits)
circ._append(Barrier(self.num_qubits), circ.qubits, circ.cregs)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
circ._append(Barrier(self.num_qubits), circ.qubits, circ.cregs)
circ._append(CircuitInstruction(Barrier(circ.num_qubits), tuple(circ.qubits), ()))

with the import of CircuitInstruction

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@itoko No problem to change this, but why is this the correct usage of the API? The type annotation for _append allows for both a CircuitInstruction or Instruction to be passed.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excuse me for my short explanation. The old code does not take cargs so it should be empty for drop-in replacement as you've already fixed so. Even if we want to change the spec and add barrier to clbits it should be circ.clbits: Sequence[Clbit] but not circ.cregs: list[ClassicalRegister]. That's why I say the above change is not correct.
As you say the use of Instruction is allowed but the new-style (using CircuitInstruction) would be preferable.

Copy link
Collaborator

@itoko itoko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, LGTM.

@itoko itoko added this pull request to the merge queue Nov 27, 2023
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Nov 27, 2023
@coruscating coruscating added this pull request to the merge queue Nov 28, 2023
Merged via the queue into qiskit-community:main with commit 2cc444a Nov 28, 2023
11 checks passed
itoko pushed a commit to itoko/qiskit-experiments that referenced this pull request Dec 12, 2023
### Summary

In this PR we improve performance of the RB circuit generation.

### Details and comments

The main performance bottleneck is the repeat application of `append` of
sequence elements to a circuit. We can improve performance by using the
`_append` for the `Barrier` gate.

For the appending of the Clifford gates we could also use `_append` (in
particular if the sequence elements are integral, in which case the
internal generation using `_to_instruction` guarantees the elements to
not need checking), but since the `StandardRB` is subclassed we have no
full information about the elements in the sequence and have chosen not
to do this.

Profiling on main:


![image](https://github.com/Qiskit-Extensions/qiskit-experiments/assets/883786/23ddd6b3-e3cb-4ade-914f-45e197b0d0c7)

PR:


![image](https://github.com/Qiskit-Extensions/qiskit-experiments/assets/883786/81714750-6f33-4410-aa4f-c14287bc8dd8)


### PR checklist (delete when all criteria are met)

- [x] I have read the contributing guide `CONTRIBUTING.md`.
- [x] I have added the tests to cover my changes.
- [x] I have updated the documentation accordingly.
- [x] I have added a release note file using `reno` if this change needs
to be documented in the release notes. Internal change, no release notes
needed
nkanazawa1989 pushed a commit to nkanazawa1989/qiskit-experiments that referenced this pull request Jan 17, 2024
### Summary

In this PR we improve performance of the RB circuit generation.

### Details and comments

The main performance bottleneck is the repeat application of `append` of
sequence elements to a circuit. We can improve performance by using the
`_append` for the `Barrier` gate.

For the appending of the Clifford gates we could also use `_append` (in
particular if the sequence elements are integral, in which case the
internal generation using `_to_instruction` guarantees the elements to
not need checking), but since the `StandardRB` is subclassed we have no
full information about the elements in the sequence and have chosen not
to do this.

Profiling on main:


![image](https://github.com/Qiskit-Extensions/qiskit-experiments/assets/883786/23ddd6b3-e3cb-4ade-914f-45e197b0d0c7)

PR:


![image](https://github.com/Qiskit-Extensions/qiskit-experiments/assets/883786/81714750-6f33-4410-aa4f-c14287bc8dd8)


### PR checklist (delete when all criteria are met)

- [x] I have read the contributing guide `CONTRIBUTING.md`.
- [x] I have added the tests to cover my changes.
- [x] I have updated the documentation accordingly.
- [x] I have added a release note file using `reno` if this change needs
to be documented in the release notes. Internal change, no release notes
needed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants