Skip to content

Commit

Permalink
Use .. plot:: directive to test code examples (Qiskit#13179)
Browse files Browse the repository at this point in the history
* Switch to plot directive

* Apply suggestions from code review

Co-authored-by: Eric Arellano <[email protected]>

* Update qiskit/transpiler/passes/scheduling/padding/pad_delay.py

Co-authored-by: Jake Lishman <[email protected]>

* Avoid writing to disk during testing

Co-authored-by: Jake Lishman <[email protected]>

---------

Co-authored-by: Eric Arellano <[email protected]>
Co-authored-by: Jake Lishman <[email protected]>
Co-authored-by: Jake Lishman <[email protected]>
  • Loading branch information
4 people authored Dec 2, 2024
1 parent 1ae287b commit 276456f
Show file tree
Hide file tree
Showing 57 changed files with 612 additions and 177 deletions.
4 changes: 3 additions & 1 deletion crates/circuit/src/circuit_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ import_exception!(qiskit.circuit.exceptions, CircuitError);
///
/// For example,
///
/// .. code-block::
/// .. plot::
/// :include-source:
/// :no-figs:
///
/// qubits = [Qubit()]
/// data = CircuitData(qubits)
Expand Down
4 changes: 3 additions & 1 deletion qiskit/assembler/assemble_circuits.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,9 @@ def assemble_circuits(
Examples:
.. code-block:: python
.. plot::
:include-source:
:nofigs:
from qiskit.circuit import QuantumRegister, ClassicalRegister, QuantumCircuit
from qiskit.assembler import assemble_circuits
Expand Down
4 changes: 3 additions & 1 deletion qiskit/assembler/assemble_schedules.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ def assemble_schedules(
Examples:
.. code-block:: python
.. plot::
:include-source:
:nofigs:
from qiskit import pulse
from qiskit.assembler import assemble_schedules
Expand Down
4 changes: 3 additions & 1 deletion qiskit/assembler/disassemble.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ def disassemble(qobj) -> Union[CircuitModule, PulseModule]:
Examples:
.. code-block:: python
.. plot::
:include-source:
:nofigs:
from qiskit.circuit import QuantumRegister, ClassicalRegister, QuantumCircuit
from qiskit.compiler.assembler import assemble
Expand Down
6 changes: 5 additions & 1 deletion qiskit/circuit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,11 @@
The :class:`Barrier` instruction can span an arbitrary number of qubits and clbits, and is a no-op
in hardware. During transpilation and optimization, however, it blocks any optimizations from
"crossing" the barrier; that is, in::
"crossing" the barrier; that is, in:
.. plot::
:include-source:
:nofigs:
from qiskit.circuit import QuantumCircuit
Expand Down
5 changes: 4 additions & 1 deletion qiskit/circuit/classical/types/ordering.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,10 @@ def cast_kind(from_: Type, to_: Type, /) -> CastKind:
Examples:
.. code-block:: python
.. plot::
:include-source:
:nofigs:
>>> from qiskit.circuit.classical import types
>>> types.cast_kind(types.Bool(), types.Bool())
Expand Down
4 changes: 3 additions & 1 deletion qiskit/circuit/classicalfunction/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
how to synthesize a simple boolean function defined using Python into a
QuantumCircuit:
.. code-block::
.. code-block:: python
from qiskit.circuit.classicalfunction import classical_function
from qiskit.circuit.classicalfunction.types import Int1
Expand All @@ -37,6 +37,8 @@ def grover_oracle(a: Int1, b: Int1, c: Int1, d: Int1) -> Int1:
quantum_circuit = grover_oracle.synth(registerless=False)
quantum_circuit.draw('text')
.. code-block:: text
a: ──o──
b: ──■──
Expand Down
12 changes: 9 additions & 3 deletions qiskit/circuit/library/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@
For example:
.. code-block::
.. plot::
:include-source:
:nofigs:
from qiskit.circuit.library import XGate
gate = XGate()
Expand Down Expand Up @@ -155,7 +157,9 @@
set the amount of qubits involved at instantiation time.
.. code-block::
.. plot::
:include-source:
:nofigs:
from qiskit.circuit.library import DiagonalGate
Expand Down Expand Up @@ -446,7 +450,9 @@
In this example, the identity constant in a template is checked:
.. code-block::
.. plot::
:include-source:
:nofigs:
from qiskit.circuit.library.templates import template_nct_4b_1
from qiskit.quantum_info import Operator
Expand Down
4 changes: 3 additions & 1 deletion qiskit/circuit/library/generalized_gates/unitary.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ class UnitaryGate(Gate):
quantum circuit. The matrix can also be directly applied to the quantum
circuit, see :meth:`.QuantumCircuit.unitary`.
.. code-block:: python
.. plot::
:include-source:
:nofigs:
from qiskit import QuantumCircuit
from qiskit.circuit.library import UnitaryGate
Expand Down
8 changes: 6 additions & 2 deletions qiskit/circuit/library/pauli_evolution.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ class PauliEvolutionGate(Gate):
**Examples:**
.. code-block:: python
.. plot::
:include-source:
:nofigs:
from qiskit.circuit import QuantumCircuit
from qiskit.circuit.library import PauliEvolutionGate
Expand All @@ -68,7 +70,9 @@ class PauliEvolutionGate(Gate):
circuit.append(evo, range(2))
print(circuit.draw())
The above will print (note that the ``-0.1`` coefficient is not printed!)::
The above will print (note that the ``-0.1`` coefficient is not printed!):
.. code-block:: text
┌──────────────────────────┐
q_0: ┤0 ├
Expand Down
66 changes: 51 additions & 15 deletions qiskit/circuit/quantumcircuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -3867,7 +3867,10 @@ def measure(self, qubit: QubitSpecifier, cbit: ClbitSpecifier) -> InstructionSet
In this example, a qubit is measured and the result of that measurement is stored in the
classical bit (usually expressed in diagrams as a double line):
.. code-block::
.. plot::
:include-source:
:nofigs:
:context: reset
from qiskit import QuantumCircuit
circuit = QuantumCircuit(1, 1)
Expand All @@ -3887,12 +3890,18 @@ def measure(self, qubit: QubitSpecifier, cbit: ClbitSpecifier) -> InstructionSet
It is possible to call ``measure`` with lists of ``qubits`` and ``cbits`` as a shortcut
for one-to-one measurement. These two forms produce identical results:
.. code-block::
.. plot::
:include-source:
:nofigs:
:context:
circuit = QuantumCircuit(2, 2)
circuit.measure([0,1], [0,1])
.. code-block::
.. plot::
:include-source:
:nofigs:
:context:
circuit = QuantumCircuit(2, 2)
circuit.measure(0, 0)
Expand All @@ -3901,7 +3910,10 @@ def measure(self, qubit: QubitSpecifier, cbit: ClbitSpecifier) -> InstructionSet
Instead of lists, you can use :class:`~qiskit.circuit.QuantumRegister` and
:class:`~qiskit.circuit.ClassicalRegister` under the same logic.
.. code-block::
.. plot::
:include-source:
:nofigs:
:context: reset
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
qreg = QuantumRegister(2, "qreg")
Expand All @@ -3911,7 +3923,10 @@ def measure(self, qubit: QubitSpecifier, cbit: ClbitSpecifier) -> InstructionSet
This is equivalent to:
.. code-block::
.. plot::
:include-source:
:nofigs:
:context:
circuit = QuantumCircuit(qreg, creg)
circuit.measure(qreg[0], creg[0])
Expand Down Expand Up @@ -4160,7 +4175,9 @@ def parameters(self) -> ParameterView:
The snippet below shows that insertion order of parameters does not matter.
.. code-block:: python
.. plot::
:include-source:
:nofigs:
>>> from qiskit.circuit import QuantumCircuit, Parameter
>>> a, b, elephant = Parameter("a"), Parameter("b"), Parameter("elephant")
Expand All @@ -4174,7 +4191,9 @@ def parameters(self) -> ParameterView:
Bear in mind that alphabetical sorting might be unintuitive when it comes to numbers.
The literal "10" comes before "2" in strict alphabetical sorting.
.. code-block:: python
.. plot::
:include-source:
:nofigs:
>>> from qiskit.circuit import QuantumCircuit, Parameter
>>> angles = [Parameter("angle_1"), Parameter("angle_2"), Parameter("angle_10")]
Expand All @@ -4189,7 +4208,9 @@ def parameters(self) -> ParameterView:
To respect numerical sorting, a :class:`.ParameterVector` can be used.
.. code-block:: python
.. plot::
:include-source:
:nofigs:
>>> from qiskit.circuit import QuantumCircuit, Parameter, ParameterVector
>>> x = ParameterVector("x", 12)
Expand Down Expand Up @@ -5720,7 +5741,9 @@ def prepare_state(
Examples:
Prepare a qubit in the state :math:`(|0\rangle - |1\rangle) / \sqrt{2}`.
.. code-block::
.. plot::
:include-source:
:nofigs:
import numpy as np
from qiskit import QuantumCircuit
Expand All @@ -5743,7 +5766,9 @@ def prepare_state(
More information about labels for basis states are in
:meth:`.Statevector.from_label`.
.. code-block::
.. plot::
:include-source:
:nofigs:
import numpy as np
from qiskit import QuantumCircuit
Expand All @@ -5764,7 +5789,10 @@ def prepare_state(
Initialize two qubits from an array of complex amplitudes
.. code-block::
.. plot::
:include-source:
:nofigs:
import numpy as np
from qiskit import QuantumCircuit
Expand Down Expand Up @@ -5835,7 +5863,9 @@ class to prepare the qubits in a specified state.
Examples:
Prepare a qubit in the state :math:`(|0\rangle - |1\rangle) / \sqrt{2}`.
.. code-block::
.. plot::
:include-source:
:nofigs:
import numpy as np
from qiskit import QuantumCircuit
Expand All @@ -5858,7 +5888,9 @@ class to prepare the qubits in a specified state.
More information about labels for basis states are in
:meth:`.Statevector.from_label`.
.. code-block::
.. plot::
:include-source:
:nofigs:
import numpy as np
from qiskit import QuantumCircuit
Expand All @@ -5879,7 +5911,9 @@ class to prepare the qubits in a specified state.
Initialize two qubits from an array of complex amplitudes.
.. code-block::
.. plot::
:include-source:
:nofigs:
import numpy as np
from qiskit import QuantumCircuit
Expand Down Expand Up @@ -5930,7 +5964,9 @@ def unitary(
Apply a gate specified by a unitary matrix to a quantum circuit
.. code-block:: python
.. plot::
:include-source:
:nofigs:
from qiskit import QuantumCircuit
matrix = [[0, 0, 0, 1],
Expand Down
4 changes: 3 additions & 1 deletion qiskit/converters/circuit_to_dag.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ def circuit_to_dag(circuit, copy_operations=True, *, qubit_order=None, clbit_ord
the circuit.
Example:
.. code-block::
.. plot::
:include-source:
:nofigs:
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
from qiskit.dagcircuit import DAGCircuit
Expand Down
4 changes: 3 additions & 1 deletion qiskit/converters/circuit_to_instruction.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ def circuit_to_instruction(circuit, parameter_map=None, equivalence_library=None
yield the components comprising the original circuit.
Example:
.. code-block::
.. plot::
:include-source:
:nofigs:
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
from qiskit.converters import circuit_to_instruction
Expand Down
Loading

0 comments on commit 276456f

Please sign in to comment.