From c53c1c82905d73109496b09947a5c3adf6afcc14 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Tue, 20 Aug 2024 10:27:24 +0000 Subject: [PATCH] Add code example in Operator docstring. (#12985) (#12991) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Elena Peña Tapia <57907331+ElePT@users.noreply.github.com> (cherry picked from commit 393fb595178567d11a534a0d9da699b0fa15d565) Co-authored-by: jschuhmac <74537145+jschuhmac@users.noreply.github.com> Co-authored-by: Elena Peña Tapia --- qiskit/quantum_info/operators/operator.py | 24 +++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/qiskit/quantum_info/operators/operator.py b/qiskit/quantum_info/operators/operator.py index a4e93f364809..42593626f2cc 100644 --- a/qiskit/quantum_info/operators/operator.py +++ b/qiskit/quantum_info/operators/operator.py @@ -55,6 +55,30 @@ class Operator(LinearOp): .. math:: \rho \mapsto M \rho M^\dagger. + + For example, the following operator :math:`M = X` applied to the zero state + :math:`|\psi\rangle=|0\rangle (\rho = |0\rangle\langle 0|)` changes it to the + one state :math:`|\psi\rangle=|1\rangle (\rho = |1\rangle\langle 1|)`: + + .. code-block:: python + + >>> import numpy as np + >>> from qiskit.quantum_info import Operator + >>> op = Operator(np.array([[0.0, 1.0], [1.0, 0.0]])) # Represents Pauli X operator + + >>> from qiskit.quantum_info import Statevector + >>> sv = Statevector(np.array([1.0, 0.0])) + >>> sv.evolve(op) + Statevector([0.+0.j, 1.+0.j], + dims=(2,)) + + >>> from qiskit.quantum_info import DensityMatrix + >>> dm = DensityMatrix(np.array([[1.0, 0.0], [0.0, 0.0]])) + >>> dm.evolve(op) + DensityMatrix([[0.+0.j, 0.+0.j], + [0.+0.j, 1.+0.j]], + dims=(2,)) + """ def __init__(