From b4125b030fd6d5e99a40224fc762defc93fce45d Mon Sep 17 00:00:00 2001 From: Frank Harkins Date: Wed, 18 Sep 2024 10:28:43 +0100 Subject: [PATCH 1/2] Fix code examples --- qiskit/circuit/library/pauli_evolution.py | 1 + qiskit/primitives/base/base_estimator.py | 4 ++-- qiskit/primitives/containers/data_bin.py | 10 +++++++- qiskit/pulse/builder.py | 23 +++++++++++++------ qiskit/pulse/instructions/directives.py | 5 ++++ qiskit/pulse/schedule.py | 16 ++++++------- qiskit/pulse/transforms/alignments.py | 4 +++- qiskit/pulse/transforms/dag.py | 7 ++++++ .../operators/symplectic/pauli.py | 2 ++ .../operators/symplectic/sparse_pauli_op.py | 6 +++++ qiskit/transpiler/__init__.py | 3 +++ .../passes/calibration/rx_builder.py | 2 +- .../passes/scheduling/padding/pad_delay.py | 3 +++ .../pass_manager_visualization.py | 17 +++----------- 14 files changed, 68 insertions(+), 35 deletions(-) diff --git a/qiskit/circuit/library/pauli_evolution.py b/qiskit/circuit/library/pauli_evolution.py index b0af3fbe4163..23ccd065aad7 100644 --- a/qiskit/circuit/library/pauli_evolution.py +++ b/qiskit/circuit/library/pauli_evolution.py @@ -56,6 +56,7 @@ class PauliEvolutionGate(Gate): X = SparsePauliOp("X") Z = SparsePauliOp("Z") + I = SparsePauliOp("I") # build the evolution gate operator = (Z ^ Z) - 0.1 * (X ^ I) diff --git a/qiskit/primitives/base/base_estimator.py b/qiskit/primitives/base/base_estimator.py index d3d364f9951a..5171a73ffc76 100644 --- a/qiskit/primitives/base/base_estimator.py +++ b/qiskit/primitives/base/base_estimator.py @@ -92,7 +92,7 @@ class BaseEstimatorV1(BasePrimitive, Generic[T]): # calculate [ ] job = estimator.run([psi1], [H1], [theta1]) job_result = job.result() # It will block until the job finishes. - print(f"The primitive-job finished with result {job_result}")) + print(f"The primitive-job finished with result {job_result}") # calculate [ , # , @@ -144,7 +144,7 @@ def run( .. code-block:: python - values = parameter_values[i]. + values = parameter_values[i] Args: circuits: one or more circuit objects. diff --git a/qiskit/primitives/containers/data_bin.py b/qiskit/primitives/containers/data_bin.py index 5ea31f7510e6..9032f85ab593 100644 --- a/qiskit/primitives/containers/data_bin.py +++ b/qiskit/primitives/containers/data_bin.py @@ -34,14 +34,22 @@ class DataBin(ShapedMixin): .. code-block:: python + import numpy as np + from qiskit.primitives import DataBin, BitArray + data = DataBin( - alpha=BitArray.from_bitstrings(["0010"]), + alpha=BitArray.from_samples(["0010"]), beta=np.array([1.2]) ) print("alpha data:", data.alpha) print("beta data:", data.beta) + .. code-block:: + + alpha data: BitArray() + beta data: [1.2] + """ __slots__ = ("_data", "_shape") diff --git a/qiskit/pulse/builder.py b/qiskit/pulse/builder.py index 8767e8c4e93a..0c8391dc99ba 100644 --- a/qiskit/pulse/builder.py +++ b/qiskit/pulse/builder.py @@ -137,7 +137,6 @@ from qiskit.compiler import schedule from qiskit import pulse, QuantumCircuit - from qiskit.pulse import library from qiskit.providers.fake_provider import FakeOpenPulse2Q backend = FakeOpenPulse2Q() @@ -147,7 +146,7 @@ with pulse.build(backend) as pulse_prog: # Create a pulse. - gaussian_pulse = library.gaussian(10, 1.0, 2) + gaussian_pulse = pulse.Gaussian(10, 1.0, 2) # Get the qubit's corresponding drive channel from the backend. d0 = pulse.drive_channel(0) d1 = pulse.drive_channel(1) @@ -285,7 +284,7 @@ d0 = pulse.drive_channel(0) a0 = pulse.acquire_channel(0) - pulse.play(pulse.library.Constant(10, 1.0), d0) + pulse.play(pulse.Constant(10, 1.0), d0) pulse.delay(20, d0) pulse.shift_phase(3.14/2, d0) pulse.set_phase(3.14, d0) @@ -293,8 +292,8 @@ pulse.set_frequency(5e9, d0) with pulse.build() as temp_sched: - pulse.play(pulse.library.Gaussian(20, 1.0, 3.0), d0) - pulse.play(pulse.library.Gaussian(20, -1.0, 3.0), d0) + pulse.play(pulse.Gaussian(20, 1.0, 3.0), d0) + pulse.play(pulse.Gaussian(20, -1.0, 3.0), d0) pulse.call(temp_sched) pulse.acquire(30, a0, pulse.MemorySlot(0)) @@ -1327,7 +1326,9 @@ def frequency_offset( :emphasize-lines: 7, 16 from qiskit import pulse + from qiskit.providers.fake_provider import FakeOpenPulse2Q + backend = FakeOpenPulse2Q() d0 = pulse.DriveChannel(0) with pulse.build(backend) as pulse_prog: @@ -1969,19 +1970,23 @@ def barrier(*channels_or_qubits: chans.Channel | int, name: str | None = None): .. code-block:: import math + from qiskit import pulse + from qiskit.providers.fake_provider import FakeOpenPulse2Q + + backend = FakeOpenPulse2Q() d0 = pulse.DriveChannel(0) with pulse.build(backend) as pulse_prog: with pulse.align_right(): - pulse.call(backend.defaults.instruction_schedule_map.get('x', (1,))) + pulse.call(backend.defaults().instruction_schedule_map.get('u1', (1,))) # Barrier qubit 1 and d0. pulse.barrier(1, d0) # Due to barrier this will play before the gate on qubit 1. pulse.play(pulse.Constant(10, 1.0), d0) # This will end at the same time as the pulse above due to # the barrier. - pulse.call(backend.defaults.instruction_schedule_map.get('x', (1,))) + pulse.call(backend.defaults().instruction_schedule_map.get('u1', (1,))) .. note:: Requires the active builder context to have a backend set if qubits are barriered on. @@ -2012,6 +2017,7 @@ def macro(func: Callable): :include-source: from qiskit import pulse + from qiskit.providers.fake_provider import FakeOpenPulse2Q @pulse.macro def measure(qubit: int): @@ -2021,6 +2027,9 @@ def measure(qubit: int): return mem_slot + + backend = FakeOpenPulse2Q() + with pulse.build(backend=backend) as sched: mem_slot = measure(0) print(f"Qubit measured into {mem_slot}") diff --git a/qiskit/pulse/instructions/directives.py b/qiskit/pulse/instructions/directives.py index f1e7f9b24569..3b7ec4c5e04c 100644 --- a/qiskit/pulse/instructions/directives.py +++ b/qiskit/pulse/instructions/directives.py @@ -72,6 +72,8 @@ class TimeBlockade(Directive): .. code-block:: python + from qiskit.pulse import Schedule, Play, Constant, DriveChannel + schedule = Schedule() schedule.insert(120, Play(Constant(10, 0.1), DriveChannel(0))) @@ -79,6 +81,9 @@ class TimeBlockade(Directive): .. code-block:: python + from qiskit.pulse import ScheduleBlock, Play, Constant, DriveChannel + from qiskit.pulse.instructions import TimeBlockade + block = ScheduleBlock() block.append(TimeBlockade(120, DriveChannel(0))) block.append(Play(Constant(10, 0.1), DriveChannel(0))) diff --git a/qiskit/pulse/schedule.py b/qiskit/pulse/schedule.py index c89fe3b4e306..d7f1f0c007f4 100644 --- a/qiskit/pulse/schedule.py +++ b/qiskit/pulse/schedule.py @@ -81,6 +81,7 @@ class Schedule: .. code-block:: python + from qiskit.pulse import Schedule, Gaussian, DriveChannel, Play sched = Schedule() sched += Play(Gaussian(160, 0.1, 40), DriveChannel(0)) @@ -88,6 +89,7 @@ class Schedule: .. code-block:: python + from qiskit.pulse import Schedule, Gaussian, DriveChannel, Play sched = Schedule() sched += Play(Gaussian(160, 0.1, 40), DriveChannel(0)) << 30 @@ -95,6 +97,7 @@ class Schedule: .. code-block:: python + from qiskit.pulse import Schedule, Gaussian, DriveChannel, Play sched1 = Schedule() sched1 += Play(Gaussian(160, 0.1, 40), DriveChannel(0)) @@ -653,8 +656,6 @@ def replace( sched += pulse.Schedule(old) - sched = sched.flatten() - sched = sched.replace(old, new) assert sched == pulse.Schedule(new) @@ -899,11 +900,8 @@ class ScheduleBlock: pulse.reference("grand_child") pulse.play(pulse.Constant(200, amp2), pulse.DriveChannel(0)) - Now you assign the inner pulse program to this reference. - - .. code-block:: - - sched_outer.assign_references({("grand_child", ): sched_inner}) + # Now assign the inner pulse program to this reference + sched_outer.assign_references({("grand_child",): sched_inner}) print(sched_outer.parameters) .. parsed-literal:: @@ -1459,7 +1457,7 @@ def assign_references( from qiskit import pulse - with pulse.build() as subroutine: + with pulse.build() as nested_prog: pulse.delay(10, pulse.DriveChannel(0)) with pulse.build() as sub_prog: @@ -1490,7 +1488,7 @@ def assign_references( .. code-block:: python main_prog.assign_references({("B", ): sub_prog}, inplace=True) - main_prog.references[("B", )].assign_references({"A": nested_prog}, inplace=True) + main_prog.references[("B", )].assign_references({("A", ): nested_prog}, inplace=True) Here :attr:`.references` returns a dict-like object, and you can mutably update the nested reference of the particular subroutine. diff --git a/qiskit/pulse/transforms/alignments.py b/qiskit/pulse/transforms/alignments.py index 5e383972c255..e608b1b636d0 100644 --- a/qiskit/pulse/transforms/alignments.py +++ b/qiskit/pulse/transforms/alignments.py @@ -338,8 +338,10 @@ class AlignFunc(AlignmentKind): .. code-block:: python + import numpy as np + def udd10_pos(j): - return np.sin(np.pi*j/(2*10 + 2))**2 + return np.sin(np.pi*j/(2*10 + 2))**2 .. note:: diff --git a/qiskit/pulse/transforms/dag.py b/qiskit/pulse/transforms/dag.py index 2dc85011849e..90c434a42769 100644 --- a/qiskit/pulse/transforms/dag.py +++ b/qiskit/pulse/transforms/dag.py @@ -32,6 +32,11 @@ def block_to_dag(block: ScheduleBlock) -> rx.PyDAG: .. code-block:: python + from qiskit import pulse + + my_gaussian0 = pulse.Gaussian(100, 0.5, 20) + my_gaussian1 = pulse.Gaussian(100, 0.3, 10) + with pulse.build() as sched1: with pulse.align_left(): pulse.play(my_gaussian0, pulse.DriveChannel(0)) @@ -51,6 +56,8 @@ def block_to_dag(block: ScheduleBlock) -> rx.PyDAG: .. code-block:: python + from qiskit import pulse + with pulse.build() as sched: with pulse.align_left(): pulse.shift_phase(1.57, pulse.DriveChannel(1)) diff --git a/qiskit/quantum_info/operators/symplectic/pauli.py b/qiskit/quantum_info/operators/symplectic/pauli.py index 44ca095f8ed6..759c618b140b 100644 --- a/qiskit/quantum_info/operators/symplectic/pauli.py +++ b/qiskit/quantum_info/operators/symplectic/pauli.py @@ -144,6 +144,8 @@ class initialization (``Pauli('-iXYZ')``). A ``Pauli`` object can be .. code-block:: python + from qiskit.quantum_info import Pauli + P = Pauli('-iXYZ') print('P[0] =', repr(P[0])) diff --git a/qiskit/quantum_info/operators/symplectic/sparse_pauli_op.py b/qiskit/quantum_info/operators/symplectic/sparse_pauli_op.py index 4621019a9d81..5f5a0d26f32c 100644 --- a/qiskit/quantum_info/operators/symplectic/sparse_pauli_op.py +++ b/qiskit/quantum_info/operators/symplectic/sparse_pauli_op.py @@ -794,6 +794,8 @@ def from_list( .. code-block:: python + from qiskit.quantum_info import SparsePauliOp + # via tuples and the full Pauli string op = SparsePauliOp.from_list([("XIIZI", 1), ("IYIIY", 2)]) @@ -858,6 +860,8 @@ def from_sparse_list( .. code-block:: python + from qiskit.quantum_info import SparsePauliOp + # via triples and local Paulis with indices op = SparsePauliOp.from_sparse_list([("ZX", [1, 4], 1), ("YY", [0, 3], 2)], num_qubits=5) @@ -1053,6 +1057,8 @@ def group_commuting(self, qubit_wise: bool = False) -> list[SparsePauliOp]: .. code-block:: python + >>> from qiskit.quantum_info import SparsePauliOp + >>> op = SparsePauliOp.from_list([("XX", 2), ("YY", 1), ("IZ",2j), ("ZZ",1j)]) >>> op.group_commuting() [SparsePauliOp(["IZ", "ZZ"], coeffs=[0.+2.j, 0.+1j]), diff --git a/qiskit/transpiler/__init__.py b/qiskit/transpiler/__init__.py index 9ea0707272b8..5a1bf4efe626 100644 --- a/qiskit/transpiler/__init__.py +++ b/qiskit/transpiler/__init__.py @@ -106,6 +106,7 @@ .. code-block:: python import numpy as np + from qiskit.providers.fake_provider import GenericBackendV2 from qiskit.circuit.library import HGate, PhaseGate, RXGate, TdgGate, TGate, XGate from qiskit.transpiler import PassManager from qiskit.transpiler.passes import ( @@ -115,6 +116,7 @@ PadDynamicalDecoupling, ) + backend = GenericBackendV2(num_qubits=5) dd_sequence = [XGate(), XGate()] scheduling_pm = PassManager( [ @@ -135,6 +137,7 @@ ] ) + pass_manager = PassManager() # Add pre-layout stage to run extra logical optimization pass_manager.pre_layout = logical_opt diff --git a/qiskit/transpiler/passes/calibration/rx_builder.py b/qiskit/transpiler/passes/calibration/rx_builder.py index 45789e4cd109..8d9955ed48dd 100644 --- a/qiskit/transpiler/passes/calibration/rx_builder.py +++ b/qiskit/transpiler/passes/calibration/rx_builder.py @@ -45,7 +45,7 @@ class RXCalibrationBuilder(CalibrationBuilder): from qiskit.circuit.library import QuantumVolume from qiskit.circuit.library.standard_gates import RXGate - from calibration.rx_builder import RXCalibrationBuilder + from qiskit.transpiler.passes import RXCalibrationBuilder qv = QuantumVolume(4, 4, seed=1004) diff --git a/qiskit/transpiler/passes/scheduling/padding/pad_delay.py b/qiskit/transpiler/passes/scheduling/padding/pad_delay.py index 886bb6a37974..482183b68cbd 100644 --- a/qiskit/transpiler/passes/scheduling/padding/pad_delay.py +++ b/qiskit/transpiler/passes/scheduling/padding/pad_delay.py @@ -27,6 +27,9 @@ class PadDelay(BasePadding): .. code-block:: python + from qiskit import QuantumCircuit + from qiskit.transpiler import InstructionDurations + durations = InstructionDurations([("x", None, 160), ("cx", None, 800)]) qc = QuantumCircuit(2) diff --git a/qiskit/visualization/pass_manager_visualization.py b/qiskit/visualization/pass_manager_visualization.py index c76d0a03a6eb..1cdc1e9214e0 100644 --- a/qiskit/visualization/pass_manager_visualization.py +++ b/qiskit/visualization/pass_manager_visualization.py @@ -58,23 +58,12 @@ def pass_manager_drawer(pass_manager, filename=None, style=None, raw=False): Example: .. code-block:: - %matplotlib inline from qiskit import QuantumCircuit - from qiskit.compiler import transpile - from qiskit.transpiler import PassManager + from qiskit.transpiler import generate_preset_pass_manager from qiskit.visualization import pass_manager_drawer - from qiskit.transpiler.passes import Unroller - circ = QuantumCircuit(3) - circ.ccx(0, 1, 2) - circ.draw() - - pass_ = Unroller(['u1', 'u2', 'u3', 'cx']) - pm = PassManager(pass_) - new_circ = pm.run(circ) - new_circ.draw(output='mpl') - - pass_manager_drawer(pm, "passmanager.jpg") + pm = generate_preset_pass_manager(optimization_level=0) + pass_manager_drawer(pm) """ import pydot From ee45b8e44cf9494c8152337dba7fcb1194706e91 Mon Sep 17 00:00:00 2001 From: Frank Harkins Date: Wed, 18 Sep 2024 14:03:08 +0100 Subject: [PATCH 2/2] Incorporate feedback Co-authored-by: Jake Lishman --- qiskit/pulse/schedule.py | 2 -- qiskit/quantum_info/operators/symplectic/sparse_pauli_op.py | 1 - qiskit/transpiler/__init__.py | 6 ++++-- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/qiskit/pulse/schedule.py b/qiskit/pulse/schedule.py index d7f1f0c007f4..7ec234cffa78 100644 --- a/qiskit/pulse/schedule.py +++ b/qiskit/pulse/schedule.py @@ -89,7 +89,6 @@ class Schedule: .. code-block:: python - from qiskit.pulse import Schedule, Gaussian, DriveChannel, Play sched = Schedule() sched += Play(Gaussian(160, 0.1, 40), DriveChannel(0)) << 30 @@ -97,7 +96,6 @@ class Schedule: .. code-block:: python - from qiskit.pulse import Schedule, Gaussian, DriveChannel, Play sched1 = Schedule() sched1 += Play(Gaussian(160, 0.1, 40), DriveChannel(0)) diff --git a/qiskit/quantum_info/operators/symplectic/sparse_pauli_op.py b/qiskit/quantum_info/operators/symplectic/sparse_pauli_op.py index 5f5a0d26f32c..5699944788be 100644 --- a/qiskit/quantum_info/operators/symplectic/sparse_pauli_op.py +++ b/qiskit/quantum_info/operators/symplectic/sparse_pauli_op.py @@ -1058,7 +1058,6 @@ def group_commuting(self, qubit_wise: bool = False) -> list[SparsePauliOp]: .. code-block:: python >>> from qiskit.quantum_info import SparsePauliOp - >>> op = SparsePauliOp.from_list([("XX", 2), ("YY", 1), ("IZ",2j), ("ZZ",1j)]) >>> op.group_commuting() [SparsePauliOp(["IZ", "ZZ"], coeffs=[0.+2.j, 0.+1j]), diff --git a/qiskit/transpiler/__init__.py b/qiskit/transpiler/__init__.py index 5a1bf4efe626..954b8045d6b5 100644 --- a/qiskit/transpiler/__init__.py +++ b/qiskit/transpiler/__init__.py @@ -108,7 +108,7 @@ import numpy as np from qiskit.providers.fake_provider import GenericBackendV2 from qiskit.circuit.library import HGate, PhaseGate, RXGate, TdgGate, TGate, XGate - from qiskit.transpiler import PassManager + from qiskit.transpiler import PassManager, generate_preset_pass_manager from qiskit.transpiler.passes import ( ALAPScheduleAnalysis, CXCancellation, @@ -137,7 +137,9 @@ ] ) - pass_manager = PassManager() + pass_manager = generate_preset_pass_manager( + optimization_level=0 + ) # Add pre-layout stage to run extra logical optimization pass_manager.pre_layout = logical_opt