Skip to content

Consistent identity removal for ConsolidateBlocks#16082

Open
Cryoris wants to merge 1 commit intoQiskit:mainfrom
Cryoris:fix-consolidate-remove-identity
Open

Consistent identity removal for ConsolidateBlocks#16082
Cryoris wants to merge 1 commit intoQiskit:mainfrom
Cryoris:fix-consolidate-remove-identity

Conversation

@Cryoris
Copy link
Copy Markdown
Collaborator

@Cryoris Cryoris commented Apr 24, 2026

Update ConsolidateBlocks to also use the average gate fidelity, like RemoveIdentityEquiv and the remainder of the stack, to check whether gates are equivalent to the identity.

AI/LLM disclosure

  • I didn't use LLM tooling, or only used it privately.
  • I used the following tool to help write this PR description:
  • I used the following tool to generate or modify code:

Update `ConsolidateBlocks` to also use the average gate fidelity, like `RemoveIdentityEquiv` and the remainder of the stack, to check whether gates are equivalent to the identity.
@Cryoris Cryoris requested a review from a team as a code owner April 24, 2026 14:01
@Cryoris Cryoris added stable backport potential Make Mergify open a backport PR to the most recent stable branch on merge. Changelog: Fixed Add a "Fixed" entry in the GitHub Release changelog. labels Apr 24, 2026
@qiskit-bot
Copy link
Copy Markdown
Collaborator

One or more of the following people are relevant to this code:

  • @Qiskit/terra-core

Copy link
Copy Markdown
Member

@mtreinish mtreinish left a comment

Choose a reason for hiding this comment

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

Thanks for fixing this. Functionally the change looks good, I just have a few small inline comments.

Comment on lines +45 to 46
use crate::passes::remove_identity_equiv::{MINIMUM_TOL, average_gate_fidelity_below_tol};
use crate::passes::unitary_synthesis::{PARAM_SET, TWO_QUBIT_BASIS_SET};
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Neither here nor there but you could simplify this a bit to:

Suggested change
use crate::passes::remove_identity_equiv::{MINIMUM_TOL, average_gate_fidelity_below_tol};
use crate::passes::unitary_synthesis::{PARAM_SET, TWO_QUBIT_BASIS_SET};
use super::remove_identity_equiv::{MINIMUM_TOL, average_gate_fidelity_below_tol};
use super::unitary_synthesis::{PARAM_SET, TWO_QUBIT_BASIS_SET};

super().__init__()
self.basis_gates = None
self.basis_gate_name = None
self.approximation_degree = approximation_degree
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Just for good measure even though it's not documented so not public I'd still make this

Suggested change
self.approximation_degree = approximation_degree
self._approximation_degree = approximation_degree

just so it's abundantly clear.

fixes:
- |
Fixed an inconsistency in :class:`.ConsolidateBlocks`, which removed identity-equivalent
sequences differently to how :class:`.RemoveIdentityEquiv` and the remainder of the stack does.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
sequences differently to how :class:`.RemoveIdentityEquiv` and the remainder of the stack does.
sequences differently to how :class:`.RemoveIdentityEquiv` and Qiskit's other transpiler passes.

- |
Fixed an inconsistency in :class:`.ConsolidateBlocks`, which removed identity-equivalent
sequences differently to how :class:`.RemoveIdentityEquiv` and the remainder of the stack does.
This has been fixed and these passes now behave consistently by using the average gate
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
This has been fixed and these passes now behave consistently by using the average gate
This has been fixed and :class:`.ConsolidateBlocks` now behaves consistently by using the average gate

sequences differently to how :class:`.RemoveIdentityEquiv` and the remainder of the stack does.
This has been fixed and these passes now behave consistently by using the average gate
fidelity to check whether a sequence is close to the identity. This implies that also
sequences that are equivalent to the identity up to global phase are now being removed.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Functionally this would happen later anyway when we run UnitarySynthesis, so it only really changes the behavior of the pass in isolation.

qc.cx(0, 1)
qc.h(0)

pm = PassManager([Collect2qBlocks(), ConsolidateBlocks()])
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You technically don't need the Collect2qBlocks call anymore. If you the pass is run without the blocks list in the property set it runs the analysis itself as the initial step.

// if the performance of this pass changes over time.
const PARALLEL_THRESHOLD: usize = 50_000;

pub const MINIMUM_TOL: f64 = 1e-12;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Not critical, but does this need to public outside of transpile or would pub(crate) or pub(super) be sufficient?

@ShellyGarion ShellyGarion added this to the 2.5.0 milestone Apr 25, 2026
@ShellyGarion ShellyGarion modified the milestones: 2.5.0, 2.4.2 Apr 25, 2026
@mtreinish mtreinish removed the stable backport potential Make Mergify open a backport PR to the most recent stable branch on merge. label Apr 28, 2026
@mtreinish mtreinish modified the milestones: 2.4.2, 2.5.0 Apr 28, 2026
pm = PassManager([Collect2qBlocks(), ConsolidateBlocks()])
self.assertEqual(QuantumCircuit(5), pm.run(qc))

def test_identity_unitary_is_removed_up_to_phase(self):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

does it's also worth checking the approximation_degree parameter?
namely, that an angle close to 2pi is not removed if the tolerance is high enough?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Changelog: Fixed Add a "Fixed" entry in the GitHub Release changelog.

Projects

Status: Ready

Development

Successfully merging this pull request may close these issues.

4 participants