-
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
Add flatten option to the NLocal family #10269
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
27f4255
Add flatten option to the NLocal family
mtreinish 3d9524b
Add missing nlocal subclasses
mtreinish 05e9f9b
Add setter
mtreinish 7d25500
Fix lint
mtreinish 4634707
Merge branch 'main' into flatten-nlocal
mtreinish b56e960
Fix flatten for EvolvedOperatorAnsatz
mtreinish 06f0659
Merge branch 'main' into flatten-nlocal
mtreinish 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
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 | ||||
---|---|---|---|---|---|---|
|
@@ -90,6 +90,7 @@ def __init__( | |||||
skip_unentangled_qubits: bool = False, | ||||||
initial_state: QuantumCircuit | None = None, | ||||||
name: str | None = "nlocal", | ||||||
flatten: Optional[bool] = None, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
) -> None: | ||||||
"""Create a new n-local circuit. | ||||||
|
||||||
|
@@ -114,6 +115,13 @@ def __init__( | |||||
initial_state: A :class:`.QuantumCircuit` object which can be used to describe an initial | ||||||
state prepended to the NLocal circuit. | ||||||
name: The name of the circuit. | ||||||
flatten: Set this to ``True`` to output a flat circuit instead of nesting it inside multiple | ||||||
layers of gate objects. By default currently the contents of | ||||||
the output circuit will be wrapped in nested objects for | ||||||
cleaner visualization. However, if you're using this circuit | ||||||
for anything besides visualization its **strongly** recommended | ||||||
to set this flag to ``True`` to avoid a large performance | ||||||
overhead for parameter binding. | ||||||
|
||||||
Examples: | ||||||
TODO | ||||||
|
@@ -144,6 +152,7 @@ def __init__( | |||||
self._initial_state: QuantumCircuit | None = None | ||||||
self._initial_state_circuit: QuantumCircuit | None = None | ||||||
self._bounds: list[tuple[float | None, float | None]] | None = None | ||||||
self._flatten = flatten | ||||||
|
||||||
if int(reps) != reps: | ||||||
raise TypeError("The value of reps should be int") | ||||||
|
@@ -188,6 +197,16 @@ def num_qubits(self, num_qubits: int) -> None: | |||||
self._num_qubits = num_qubits | ||||||
self.qregs = [QuantumRegister(num_qubits, name="q")] | ||||||
|
||||||
@property | ||||||
def flatten(self) -> bool: | ||||||
"""Returns whether the circuit is wrapped in nested gates/instructions or flattened.""" | ||||||
return bool(self._flatten) | ||||||
|
||||||
@flatten.setter | ||||||
def flatten(self, flatten: bool) -> None: | ||||||
self._invalidate() | ||||||
self._flatten = flatten | ||||||
|
||||||
def _convert_to_block(self, layer: Any) -> QuantumCircuit: | ||||||
"""Try to convert ``layer`` to a QuantumCircuit. | ||||||
|
||||||
|
@@ -899,7 +918,10 @@ def _build(self) -> None: | |||||
if self.num_qubits == 0: | ||||||
return | ||||||
|
||||||
circuit = QuantumCircuit(*self.qregs, name=self.name) | ||||||
if not self._flatten: | ||||||
circuit = QuantumCircuit(*self.qregs, name=self.name) | ||||||
else: | ||||||
circuit = self | ||||||
|
||||||
# use the initial state as starting circuit, if it is set | ||||||
if self.initial_state: | ||||||
|
@@ -943,12 +965,13 @@ def _build(self) -> None: | |||||
# expression contains free parameters | ||||||
pass | ||||||
|
||||||
try: | ||||||
block = circuit.to_gate() | ||||||
except QiskitError: | ||||||
block = circuit.to_instruction() | ||||||
if not self._flatten: | ||||||
try: | ||||||
block = circuit.to_gate() | ||||||
except QiskitError: | ||||||
block = circuit.to_instruction() | ||||||
|
||||||
self.append(block, self.qubits) | ||||||
self.append(block, self.qubits) | ||||||
jakelishman marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
# pylint: disable=unused-argument | ||||||
def _parameter_generator(self, rep: int, block: int, indices: list[int]) -> Parameter | None: | ||||||
|
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 | ||||
---|---|---|---|---|---|---|
|
@@ -14,6 +14,8 @@ | |||||
|
||||||
# pylint: disable=cyclic-import | ||||||
from __future__ import annotations | ||||||
from typing import Optional | ||||||
|
||||||
import numpy as np | ||||||
|
||||||
from qiskit.circuit.library.evolved_operator_ansatz import EvolvedOperatorAnsatz, _is_pauli_identity | ||||||
|
@@ -39,6 +41,7 @@ def __init__( | |||||
initial_state: QuantumCircuit | None = None, | ||||||
mixer_operator=None, | ||||||
name: str = "QAOA", | ||||||
flatten: Optional[bool] = None, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
): | ||||||
r""" | ||||||
Args: | ||||||
|
@@ -55,8 +58,15 @@ def __init__( | |||||
in the original paper. Can be an operator or an optionally parameterized quantum | ||||||
circuit. | ||||||
name (str): A name of the circuit, default 'qaoa' | ||||||
flatten: Set this to ``True`` to output a flat circuit instead of nesting it inside multiple | ||||||
layers of gate objects. By default currently the contents of | ||||||
the output circuit will be wrapped in nested objects for | ||||||
cleaner visualization. However, if you're using this circuit | ||||||
for anything besides visualization its **strongly** recommended | ||||||
to set this flag to ``True`` to avoid a large performance | ||||||
overhead for parameter binding. | ||||||
""" | ||||||
super().__init__(reps=reps, name=name) | ||||||
super().__init__(reps=reps, name=name, flatten=flatten) | ||||||
|
||||||
self._cost_operator = None | ||||||
self._reps = reps | ||||||
|
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
22 changes: 22 additions & 0 deletions
22
releasenotes/notes/flatten-nlocal-family-292b23b99947f3c9.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,22 @@ | ||
--- | ||
features: | ||
- | | ||
Added a new keyword argument ``flatten`` to the constructor for the | ||
following classes: | ||
|
||
* :class:`~.EfficientSU2` | ||
* :class:`~.ExcitationPreserving` | ||
* :class:`~.NLocal` | ||
* :class:`~.RealAmplitudes` | ||
* :class:`~.TwoLocal` | ||
* :class:`~.EvolvedOperatorAnsatz` | ||
* :class:`~.QAOAAnsatz` | ||
|
||
If this argument is set to ``True`` the :class:`~.QuantumCircuit` subclass | ||
generated will not wrap the implementation into :class:`~.Gate` or | ||
:class:`~.circuit.Instruction` objects. While this isn't optimal for visualization | ||
it typically results in much better runtime performance, especially with | ||
:meth:`.QuantumCircuit.bind_parameters` and | ||
:meth:`.QuantumCircuit.assign_parameters` which can see a substatial | ||
runtime improvement with a flattened output compared to the nested | ||
wrapped default output. |
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
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.
For consistency with the rest of the signature (also the
Optional
import would need to be removed 🙂)