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

Add option max_block_width to CollectLinearFunctions and CollectClifford passes #13661

Merged
merged 6 commits into from
Jan 28, 2025

Conversation

alexanderivrii
Copy link
Member

@alexanderivrii alexanderivrii commented Jan 14, 2025

Summary

This PR adds a new argument max_block_width to the class BlockCollector and to the transpiler passes CollectLinearFunctions and CollectCliffords that are based on this class. This argument enforces block collection strategies to restrict the maximum number of qubits over which a block of nodes is defined.

This option is similar to the option max_block_size in the CollectMultiQBlocks transpiler pass (though naming-wise, I am calling this "width" instead of "size", which is how it should be using the terminology of DAGCircuit and QuantumCircuit).

The implementation is based on the adaptation of BlockCollector in AI resynthesis passes (including a small cleanup for updating the frontier nodes).

Details and comments

Somewhat related to the PR, here is a more refined difference between BlockCollector and CollectMultiQBlocks:

  1. BlockCollector allows to only collect nodes that match a given "filter function" and treat other nodes as "should not be collected", while CollectMultiQBlocks collects all nodes (in particular, CollectMultiQBlocks cannot be used to only collect say Clifford gates within in a larger circuit).

  2. BlockCollector can collect nodes exploiting commutativity of nodes within the DAG; CollectMultiQBlocks does not have this functionality. BlockCollector has other options that CollectMultiQBlocks does not have.

  3. Both can now collect nodes "forward-wise" from the inputs towards the ouputs of a circuit, and backward-wise from the outputs towards the inputs.

  4. Both BlockCollector and CollectMultiQBlocks are greedy, however CollectMultiQBlocks is smarter, in the following way. BlockCollector greedily collects one block at a time, greedily trying to add nodes into the block being collected. In particular it may create "disconnected" blocks . On the other hand, CollectMultiQBlocks (with the implementation based on the disjoint set union data structure) produces connected blocks by construction.

@alexanderivrii alexanderivrii requested a review from a team as a code owner January 14, 2025 12:12
@qiskit-bot
Copy link
Collaborator

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

  • @Qiskit/terra-core

@alexanderivrii alexanderivrii added the Changelog: New Feature Include in the "Added" section of the changelog label Jan 14, 2025
@alexanderivrii alexanderivrii added this to the 2.0.0 milestone Jan 14, 2025
@coveralls
Copy link

coveralls commented Jan 14, 2025

Pull Request Test Coverage Report for Build 12991606884

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 19 of 19 (100.0%) changed or added relevant lines in 1 file are covered.
  • 1284 unchanged lines in 46 files lost coverage.
  • Overall coverage increased (+0.01%) to 88.944%

Files with Coverage Reduction New Missed Lines %
crates/circuit/src/lib.rs 1 94.64%
crates/accelerate/src/basis/basis_translator/basis_search.rs 1 99.3%
qiskit/synthesis/linear/linear_depth_lnn.py 1 90.91%
crates/accelerate/src/remove_identity_equiv.rs 1 95.45%
crates/qasm3/src/build.rs 1 72.24%
crates/accelerate/src/barrier_before_final_measurement.rs 1 98.65%
crates/accelerate/src/check_map.rs 1 98.39%
crates/accelerate/src/isometry.rs 1 99.65%
crates/accelerate/src/unitary_synthesis.rs 2 93.09%
crates/qasm3/src/lib.rs 2 87.88%
Totals Coverage Status
Change from base Build 12765737818: 0.01%
Covered Lines: 79567
Relevant Lines: 89457

💛 - Coveralls

Copy link
Member

@ShellyGarion ShellyGarion left a comment

Choose a reason for hiding this comment

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

LGTM. I only have some minor comments on the tests.

@ShellyGarion
Copy link
Member

what happens if you take this circuit:

        circuit = QuantumCircuit(5)
        circuit.cx(0, 1)
        circuit.cx(3, 4)
        circuit.cx(1, 3)
        circuit.cx(2, 4)

with max_block_width=4?
do you get 3 blocks [0,1,3], [3,4],[2,4] or 2 block [0,1][1,2,3,4]?
do we care about this?

@alexanderivrii
Copy link
Member Author

what happens if you take this circuit:

    circuit = QuantumCircuit(5)
    circuit.cx(0, 1)
    circuit.cx(3, 4)
    circuit.cx(1, 3)
    circuit.cx(2, 4)

with max_block_width=4?
do you get 3 blocks [0,1,3], [3,4],[2,4] or 2 block [0,1][1,2,3,4]?
do we care about this?

In general, the problem of "optimally" dividing a circuit into blocks is "hard" (and in fact NP-hard for suitable notions of "optimality"). In practice, which blocks get collected depends on the order in which the nodes are processed and on other options given to BlockCollector (like exploiting commutativity between nodes or collecting from the back of the circuit). So the result is generally not unique. In fact, for the example above, with the default options, we would collect the first 3 gates into one block, and the remaining gate into the second block, and with collect_from_back=True, we would collect the last 3 gates into one block, and the first gate into the second block.

I would say we should care about this when we think about resynthesis, as collecting different blocks gives rise to different resynthesis opportunities.

Copy link
Member

@ShellyGarion ShellyGarion left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks @alexanderivrii

@ShellyGarion ShellyGarion added this pull request to the merge queue Jan 28, 2025
Merged via the queue into Qiskit:main with commit b9e9b41 Jan 28, 2025
17 checks passed
emilkovacev pushed a commit to emilkovacev/qiskit that referenced this pull request Feb 7, 2025
…llectClifford`` passes (Qiskit#13661)

* new option max_block_width for CollectCliffords and CollectLinearFunctions passes

* release notes

* improving release notes

* refactoring tests as per review comments

* renaming max_width to max_block_width in comment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: New Feature Include in the "Added" section of the changelog
Projects
Status: done
Development

Successfully merging this pull request may close these issues.

4 participants