Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@
"sphinx.ext.viewcode",
"sphinx.ext.extlinks",
"sphinx.ext.intersphinx",
"jupyter_sphinx",
"sphinx_autodoc_typehints",
"reno.sphinxext",
"sphinx_design",
"matplotlib.sphinxext.plot_directive",
]
templates_path = ["_templates"]

Expand Down
4 changes: 1 addition & 3 deletions qiskit/algorithms/evolvers/trotterization/trotter_qrte.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ class TrotterQRTE(RealEvolver):

Type of Trotterization is defined by a ProductFormula provided.

Examples:

.. jupyter-execute::
Examples::

from qiskit.opflow import X, Z, Zero
from qiskit.algorithms import EvolutionProblem, TrotterQRTE
Expand Down
4 changes: 1 addition & 3 deletions qiskit/algorithms/linear_solvers/hhl.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ class HHL(LinearSolver):
It is replaced by the tutorial at
`HHL <https://qiskit.org/textbook/ch-applications/hhl_tutorial.html>`_

Examples:

.. jupyter-execute::
Examples::

import warnings
import numpy as np
Expand Down
4 changes: 1 addition & 3 deletions qiskit/algorithms/linear_solvers/matrices/numpy_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@
class NumPyMatrix(LinearSystemMatrix):
"""The deprecated class of matrices given as a numpy array.

Examples:

.. jupyter-execute::
Examples::

import warnings
import numpy as np
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ class TridiagonalToeplitz(LinearSystemMatrix):
0 & 0 & b & a
\end{pmatrix}.

Examples:

.. jupyter-execute::
Examples::

import warnings
import numpy as np
Expand Down
4 changes: 1 addition & 3 deletions qiskit/algorithms/linear_solvers/numpy_linear_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ class NumPyLinearSolver(LinearSolver):
This linear system solver computes the exact value of the given observable(s) or the full
solution vector if no observable is specified.

Examples:

.. jupyter-execute::
Examples::

import warnings
import numpy as np
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ class AbsoluteAverage(LinearSystemObservable):
For a vector :math:`x=(x_1,...,x_N)`, the absolute average is defined as
:math:`\abs{\frac{1}{N}\sum_{i=1}^{N}x_i}`.

Examples:

.. jupyter-execute::
Examples::

import warnings
import numpy as np
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@
class MatrixFunctional(LinearSystemObservable):
"""The deprecated class for the matrix functional of the vector solution to the linear systems.

Examples:

.. jupyter-execute::
Examples::

import warnings
import numpy as np
Expand Down
63 changes: 49 additions & 14 deletions qiskit/circuit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
|\\psi\\rangle = \\left(|000\\rangle+|111\\rangle\\right)/\\sqrt{2}


.. jupyter-execute::
.. plot::
:include-source:

from qiskit import QuantumCircuit
# Create a circuit with a register of three qubits
Expand All @@ -50,7 +51,7 @@
# CX (CNOT) gate on control qubit 0 and target qubit 2 resulting in a GHZ state.
circ.cx(0, 2)
# Draw the circuit
circ.draw()
circ.draw('mpl')


Supplementary Information
Expand All @@ -67,9 +68,10 @@
(:class:`~.HGate`), in which we expect to get :math:`|0\\rangle` and :math:`|1\\rangle`
with equal probability.

.. jupyter-execute::
.. plot::
:include-source:

from qiskit import BasicAer, transpile, QuantumRegister, ClassicalRegister
from qiskit import BasicAer, transpile, QuantumRegister, ClassicalRegister, QuantumCircuit

qr = QuantumRegister(1)
cr = ClassicalRegister(1)
Expand All @@ -78,33 +80,50 @@
qc.measure(0, 0)
qc.draw('mpl')

.. jupyter-execute::
.. code-block::

backend = BasicAer.get_backend('qasm_simulator')
tqc = transpile(qc, backend)
counts = backend.run(tqc).result().get_counts()

print(counts)

.. parsed-literal::

{'0': 524, '1': 500}

Now, we add an :class:`~.XGate` only if the value of the :class:`~.ClassicalRegister` is 0.
That way, if the state is :math:`|0\\rangle`, it will be changed to :math:`|1\\rangle` and
if the state is :math:`|1\\rangle`, it will not be changed at all, so the final state will
always be :math:`|1\\rangle`.

.. jupyter-execute::
.. plot::
:include-source:

from qiskit import BasicAer, transpile, QuantumRegister, ClassicalRegister, QuantumCircuit

qr = QuantumRegister(1)
cr = ClassicalRegister(1)
qc = QuantumCircuit(qr, cr)
qc.h(0)
qc.measure(0, 0)

qc.x(0).c_if(cr, 0)
qc.measure(0, 0)

qc.draw('mpl')

.. jupyter-execute::
.. code-block::

backend = BasicAer.get_backend('qasm_simulator')
tqc = transpile(qc, backend)
counts = backend.run(tqc).result().get_counts()

print(counts)

.. parsed-literal::

{'1': 1024}

.. dropdown:: Quantum Circuit Properties
:animate: fade-in-slide-down
Expand All @@ -118,7 +137,8 @@

Consider the following circuit:

.. jupyter-execute::
.. plot::
:include-source:

from qiskit import QuantumCircuit
qc = QuantumCircuit(12)
Expand All @@ -135,24 +155,30 @@
qc.swap(6, 9)
qc.swap(6, 10)
qc.x(6)
qc.draw()
qc.draw('mpl')

From the plot, it is easy to see that this circuit has 12 qubits, and a collection of
Hadamard, CNOT, X, and SWAP gates. But how to quantify this programmatically? Because we
can do single-qubit gates on all the qubits simultaneously, the number of qubits in this
circuit is equal to the **width** of the circuit:

.. jupyter-execute::
.. code-block::

qc.width()

.. parsed-literal::

12

We can also just get the number of qubits directly:

.. jupyter-execute::
.. code-block::

qc.num_qubits

.. parsed-literal::

12

.. important::

Expand All @@ -166,18 +192,24 @@
It is also straightforward to get the number and type of the gates in a circuit using
:meth:`QuantumCircuit.count_ops`:

.. jupyter-execute::
.. code-block::

qc.count_ops()

.. parsed-literal::

OrderedDict([('cx', 8), ('h', 5), ('x', 3), ('swap', 3)])

We can also get just the raw count of operations by computing the circuits
:meth:`QuantumCircuit.size`:

.. jupyter-execute::
.. code-block::

qc.size()

.. parsed-literal::

19

A particularly important circuit property is known as the circuit **depth**. The depth
of a quantum circuit is a measure of how many "layers" of quantum gates, executed in
Expand All @@ -202,10 +234,13 @@

We can verify our graphical result using :meth:`QuantumCircuit.depth`:

.. jupyter-execute::
.. code-block::

qc.depth()

.. parsed-literal::

9

.. raw:: html

Expand Down
3 changes: 1 addition & 2 deletions 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:

.. jupyter-execute::
.. code-block::

from qiskit.circuit.classicalfunction import classical_function
from qiskit.circuit.classicalfunction.types import Int1
Expand All @@ -35,7 +35,6 @@ def grover_oracle(a: Int1, b: Int1, c: Int1, d: Int1) -> Int1:
return (not a and b and not c and d)

quantum_circuit = grover_oracle.synth()
quantum_circuit.draw()

Following Qiskit's little-endian bit ordering convention, the left-most bit (`a`) is the most
significant bit and the right-most bit (`d`) is the least significant bit. The resulting
Expand Down
10 changes: 6 additions & 4 deletions qiskit/circuit/controlledgate.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ def __init__(

Create a controlled standard gate and apply it to a circuit.

.. jupyter-execute::
.. plot::
:include-source:

from qiskit import QuantumCircuit, QuantumRegister
from qiskit.circuit.library.standard_gates import HGate
Expand All @@ -73,11 +74,12 @@ def __init__(
qc = QuantumCircuit(qr)
c3h_gate = HGate().control(2)
qc.append(c3h_gate, qr)
qc.draw()
qc.draw('mpl')

Create a controlled custom gate and apply it to a circuit.

.. jupyter-execute::
.. plot::
:include-source:

from qiskit import QuantumCircuit, QuantumRegister
from qiskit.circuit.library.standard_gates import HGate
Expand All @@ -89,7 +91,7 @@ def __init__(

qc2 = QuantumCircuit(4)
qc2.append(custom, [0, 3, 1, 2])
qc2.draw()
qc2.draw('mpl')
"""
self.base_gate = None if base_gate is None else base_gate.copy()
super().__init__(name, num_qubits, params, label=label)
Expand Down
25 changes: 13 additions & 12 deletions qiskit/circuit/instructionset.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,23 +188,24 @@ def c_if(self, classical: Union[Clbit, ClassicalRegister, int], val: int) -> "In
to a concrete resource that these instructions are permitted to access.

Example:
.. jupyter-execute::
.. plot::
:include-source:

from qiskit import ClassicalRegister, QuantumRegister, QuantumCircuit
from qiskit import ClassicalRegister, QuantumRegister, QuantumCircuit

qr = QuantumRegister(2)
cr = ClassicalRegister(2)
qc = QuantumCircuit(qr, cr)
qc.h(range(2))
qc.measure(range(2), range(2))
qr = QuantumRegister(2)
cr = ClassicalRegister(2)
qc = QuantumCircuit(qr, cr)
qc.h(range(2))
qc.measure(range(2), range(2))

# apply x gate if the classical register has the value 2 (10 in binary)
qc.x(0).c_if(cr, 2)
# apply x gate if the classical register has the value 2 (10 in binary)
qc.x(0).c_if(cr, 2)

# apply y gate if bit 0 is set to 1
qc.y(1).c_if(0, 1)
# apply y gate if bit 0 is set to 1
qc.y(1).c_if(0, 1)

qc.draw()
qc.draw('mpl')

"""
if self._requester is None and not isinstance(classical, (Clbit, ClassicalRegister)):
Expand Down
Loading