-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add apply_layout method to SparsePauliOp
This commit adds a new method, `apply_layout`, to the `SparsePauliOp` class. It takes in either a `TranspileLayout` object or a list of indices that represent a layout transformation caused by the transpiler and then returns a new SparsePauliOp object that applies a matching transformation.
- Loading branch information
Showing
3 changed files
with
136 additions
and
0 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
33 changes: 33 additions & 0 deletions
33
releasenotes/notes/sparse-pauli-op-apply-layout-43149125d29ad015.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,33 @@ | ||
--- | ||
features: | ||
- | | ||
Added a new method, :meth:`~.SparsePauliOp.apply_layout`, | ||
to the :class:~.SparsePauliOp` class. This method is used to take | ||
a :class:`~.TranspileLayout` observable for a given input circuit and permute | ||
it based on the layout from the transpiler. This enables working with | ||
the :class:`~.BaseEstimator` implementations and local transpilation more | ||
easily. For example:: | ||
from qiskit.circuit.library import RealAmplitudes | ||
from qiskit.quantum_info import SparsePauliOp | ||
from qiskit.primitives import BackendEstimator | ||
from qiskit.compiler import transpile | ||
from qiskit.providers.fake_provider import FakeNairobiV2 | ||
psi = RealAmplitudes(num_qubits=2, reps=2) | ||
H1 = SparsePauliOp.from_list([("II", 1), ("IZ", 2), ("XI", 3)]) | ||
backend = FakeNairobiV2() | ||
estimator = BackendEstimator(backend=backend, skip_transpilation=True) | ||
thetas = [0, 1, 1, 2, 3, 5] | ||
transpiled_psi = transpile(psi, backend, optimization_level=3) | ||
permuted_op = H1.apply_layout(transpiled_psi.layout) | ||
res = estimator.run(transpiled_psi, permuted_op, thetas) | ||
where you locally transpile the input circuit before passing it to | ||
:class:`~.BaseEstimator.run`, the transpiled circuit will be expanded from | ||
2 qubits to 7 qubits and the qubits will be permuted as part of | ||
transpilation. Using :meth:`~.SparsePauliOp.apply_layout` | ||
transforms ``H1`` which was constructed assuming the original untranspiled | ||
circuit to reflect the transformations :func:`~.transpile` performed on | ||
the circuit. |
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