-
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
Optimize ConsolidateBlocks
pass
#10365
Merged
Merged
Changes from 17 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
ccf850c
Initial: Introducing speedup by calculating matrix
raynelfss 6aac6c0
Lint: Reformat using Black
raynelfss 1839808
Fix: Added `basis_count` and `outside_basis`
raynelfss 636144c
Merge branch 'main' into blocks-optimize
raynelfss 653e23c
Fix: Use `Operator` if `to_matrix` is unavailable.
raynelfss b529786
Merge branch 'main' into blocks-optimize
raynelfss cfe873f
Fix: Default to Operator when necessary
raynelfss 1c3278a
Lint: Removed Cyclic import
raynelfss 76ac395
Docs: Added release note.
raynelfss 410e0ad
Docs: Added reference to the issue.
raynelfss a990f75
Fix: Move `block_to_matrix` to ~.passes.utils
raynelfss b1eab99
Merge branch 'Qiskit:main' into blocks-optimize
raynelfss 881a442
Lint: Fixed cyclical import and order
raynelfss c3e25d6
Merge branch 'main' into blocks-optimize
raynelfss 6d7387a
CI: Removed type checking
raynelfss 5b93147
Merge branch 'blocks-optimize' of https://github.com/raynelfss/qiskit…
raynelfss c87d00c
Merge branch 'main' into blocks-optimize
raynelfss 128df96
Add: Exceptions on `_block_to_matrix`
raynelfss b252d7a
Docs: Fix release note.
raynelfss f6f2cb0
Fix: Change import path for block_to_matrix
raynelfss 07b3c88
Update qiskit/transpiler/passes/utils/block_to_matrix.py
ewinston 7e0d1ad
Merge branch 'main' into blocks-optimize
raynelfss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# This code is part of Qiskit. | ||
# | ||
# (C) Copyright IBM 2017, 2018. | ||
# | ||
# This code is licensed under the Apache License, Version 2.0. You may | ||
# obtain a copy of this license in the LICENSE.txt file in the root directory | ||
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. | ||
# | ||
# Any modifications or derivative works of this code must retain this | ||
# copyright notice, and modified files need to carry a notice indicating | ||
# that they have been altered from the originals. | ||
|
||
"""Converts any block of 2 qubit gates into a matrix.""" | ||
|
||
from numpy import identity, kron | ||
from qiskit.circuit.library import SwapGate | ||
from qiskit.quantum_info import Operator | ||
from qiskit.exceptions import QiskitError | ||
|
||
|
||
SWAP_GATE = SwapGate() | ||
SWAP_MATRIX = SWAP_GATE.to_matrix() | ||
IDENTITY = identity(2, dtype=complex) | ||
|
||
|
||
def _block_to_matrix(block, block_index_map): | ||
""" | ||
The function converts any sequence of operations between two qubits into a matrix | ||
that can be utilized to create a gate or a unitary. | ||
|
||
Args: | ||
block (List(DAGOpNode)): A block of operations on two qubits. | ||
block_index_map (dict(Qubit, int)): The mapping of the qubit indices in the main circuit. | ||
|
||
Returns: | ||
NDArray: Matrix representation of the block of operations. | ||
""" | ||
matrix = identity(2 ** len(block_index_map), dtype=complex) | ||
for node in block: | ||
try: | ||
current = node.op.to_matrix() | ||
except QiskitError: | ||
current = Operator(node.op).data | ||
q_list = [block_index_map[qubit] for qubit in node.qargs] | ||
basis_change = False | ||
if len(q_list) < 2: | ||
if q_list[0] == 1: | ||
current = kron(current, IDENTITY) | ||
else: | ||
current = kron(IDENTITY, current) | ||
else: | ||
if q_list[0] > q_list[1]: | ||
if node.op != SWAP_GATE: | ||
basis_change = True | ||
if basis_change: | ||
matrix = (SWAP_MATRIX @ current) @ (SWAP_MATRIX @ matrix) | ||
else: | ||
matrix = current @ matrix | ||
return matrix |
9 changes: 9 additions & 0 deletions
9
releasenotes/notes/optimize-consolidate-blocks-3ea60c18bc546273.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
fixes: | ||
- | | ||
Reduced overhead of the :class:`.ConsolidateBlocks` pass by performing matrix operations | ||
on all two-qubit blocks privately instead of creating an instance of :class:`.QuantumCircuit` | ||
and passing it to an :class:`.Operator`. The speedup will only be applicable when | ||
consolidating two-qubit blocks. Anything higher than that will still be handled by the | ||
:class:`.Operator` class. | ||
Check `#8779 <https://github.com/Qiskit/qiskit-terra/issues/8779>`__ for details. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
An exception should be raised if the dimension of matrix != 4.
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.
For this, it would be better to just throw an exception whenever
len(block_index_map) != 2
. Since earlier in the code we know thatblock_index_map
comes from the following operation:https://github.com/Qiskit/qiskit-terra/blob/c87d00c90af0635561c97a8ae54cd7eeccf78fce/qiskit/transpiler/passes/optimization/consolidate_blocks.py#L106
Which takes
block_qargs
(The set of all qubits in the block) as an argument and returns a mapped dictionary of the same length. Before we call_block_to_matrix
we will check thatlen(block_qargs) <= 2
to call it, otherwise, the old method will be used. As shown here:https://github.com/Qiskit/qiskit-terra/blob/c87d00c90af0635561c97a8ae54cd7eeccf78fce/qiskit/transpiler/passes/optimization/consolidate_blocks.py#L112-L123
So it would be better to just add an exception at the beginning of
block_to_matrix
that says if the length ofblock_index_length
exceeds 2 then throw an error. Which would cover for cases in which the matrix has a dimension that exceeds 2x2.