forked from Qiskit/qiskit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Deprecate
qiskit.extensions
(Qiskit#10725)
* big moves, import still works * most tests pass (some I cannot seem to run locally) * fix tests -- how to remove DiagonalGate? * typehints and docs * more type hints * Deprecate SQU * deprecate Snapshot * Fix missing future annotations import * minimize deprecation effort * Change to pending deprecation, no exact-location import supported * fix MCG<->MGC typo and snapshot deprecation * fix pylint, try fixing docs * remove gates from extensions toctree * Add reno, fully deprecate SQU and Snapshot * Apply Sasha's review comments - fix usage of .squ and .snapshot w/o import - fix docstring usage of extensions - fix tests * capture snapshot deprecation warning * review comments - capture warning of SQU - update reno to explicitly mention pending deprecation, add DiagonalGate and ExtensionError * missed `diagonal` method --------- Co-authored-by: Alexander Ivrii <[email protected]>
- Loading branch information
Showing
81 changed files
with
1,453 additions
and
1,297 deletions.
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
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
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,96 @@ | ||
# This code is part of Qiskit. | ||
# | ||
# (C) Copyright IBM 2017. | ||
# | ||
# 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. | ||
|
||
""" | ||
Initialize qubit registers to desired arbitrary state. | ||
""" | ||
|
||
from __future__ import annotations | ||
from collections.abc import Sequence | ||
import typing | ||
|
||
from qiskit.circuit.quantumcircuit import QuantumCircuit | ||
from qiskit.circuit.quantumregister import QuantumRegister | ||
from qiskit.circuit.instruction import Instruction | ||
from .state_preparation import StatePreparation | ||
|
||
if typing.TYPE_CHECKING: | ||
from qiskit.quantum_info.states.statevector import Statevector | ||
|
||
_EPS = 1e-10 # global variable used to chop very small numbers to zero | ||
|
||
|
||
class Initialize(Instruction): | ||
"""Complex amplitude initialization. | ||
Class that initializes some flexible collection of qubit registers, implemented by calling | ||
the :class:`~.library.StatePreparation` class. | ||
Note that ``Initialize`` is an :class:`~.circuit.Instruction` and not a :class:`.Gate` since it | ||
contains a reset instruction, which is not unitary. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
params: Statevector | Sequence[complex] | str | int, | ||
num_qubits: int | None = None, | ||
normalize: bool = False, | ||
) -> None: | ||
r""" | ||
Args: | ||
params: The state to initialize to, can be either of the following. | ||
* Statevector or vector of complex amplitudes to initialize to. | ||
* Labels of basis states of the Pauli eigenstates Z, X, Y. See | ||
:meth:`.Statevector.from_label`. Notice the order of the labels is reversed with | ||
respect to the qubit index to be applied to. Example label '01' initializes the | ||
qubit zero to :math:`|1\rangle` and the qubit one to :math:`|0\rangle`. | ||
* An integer that is used as a bitmap indicating which qubits to initialize to | ||
:math:`|1\rangle`. Example: setting params to 5 would initialize qubit 0 and qubit | ||
2 to :math:`|1\rangle` and qubit 1 to :math:`|0\rangle`. | ||
num_qubits: This parameter is only used if params is an int. Indicates the total | ||
number of qubits in the `initialize` call. Example: `initialize` covers 5 qubits | ||
and params is 3. This allows qubits 0 and 1 to be initialized to :math:`|1\rangle` | ||
and the remaining 3 qubits to be initialized to :math:`|0\rangle`. | ||
normalize: Whether to normalize an input array to a unit vector. | ||
""" | ||
self._stateprep = StatePreparation(params, num_qubits, normalize=normalize) | ||
|
||
super().__init__("initialize", self._stateprep.num_qubits, 0, self._stateprep.params) | ||
|
||
def _define(self): | ||
q = QuantumRegister(self.num_qubits, "q") | ||
initialize_circuit = QuantumCircuit(q, name="init_def") | ||
initialize_circuit.reset(q) | ||
initialize_circuit.append(self._stateprep, q) | ||
self.definition = initialize_circuit | ||
|
||
def gates_to_uncompute(self) -> QuantumCircuit: | ||
"""Call to create a circuit with gates that take the desired vector to zero. | ||
Returns: | ||
Circuit to take ``self.params`` vector to :math:`|{00\\ldots0}\\rangle` | ||
""" | ||
return self._stateprep._gates_to_uncompute() | ||
|
||
@property | ||
def params(self): | ||
"""Return initialize params.""" | ||
return self._stateprep.params | ||
|
||
@params.setter | ||
def params(self, parameters: Statevector | Sequence[complex] | str | int) -> None: | ||
"""Set initialize params.""" | ||
self._stateprep.params = parameters | ||
|
||
def broadcast_arguments(self, qargs, cargs): | ||
return self._stateprep.broadcast_arguments(qargs, cargs) |
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
Oops, something went wrong.