diff --git a/docs/how_to/compose_quantum_circuits.rst b/docs/how_to/compose_quantum_circuits.rst new file mode 100644 index 000000000000..c78214196ec2 --- /dev/null +++ b/docs/how_to/compose_quantum_circuits.rst @@ -0,0 +1,184 @@ +##################### +Join quantum circuits +##################### + +This guide shows you how to combine different :class:`~.QuantumCircuit` objects. + +Create the circuits +=================== + +Let's first create two circuits, which will be joined together. + +.. testcode:: + + from qiskit import QuantumCircuit + + qc1 = QuantumCircuit(2,1) + qc1.h(0) + qc1.cx(0,1) + qc1.measure(1,0) + + qc2 = QuantumCircuit(4,2) + qc2.y(0) + qc2.x(1) + qc2.cx(0,3) + qc2.measure(3,1) + + print(qc1.draw()) + print(qc2.draw()) + +.. testoutput:: + :options: +NORMALIZE_WHITESPACE + + ┌───┐ + q_0: ┤ H ├──■───── + └───┘┌─┴─┐┌─┐ + q_1: ─────┤ X ├┤M├ + └───┘└╥┘ + c: 1/═══════════╩═ + 0 + ┌───┐ + q_0: ┤ Y ├──■───── + ├───┤ │ + q_1: ┤ X ├──┼───── + └───┘ │ + q_2: ───────┼───── + ┌─┴─┐┌─┐ + q_3: ─────┤ X ├┤M├ + └───┘└╥┘ + c: 2/═══════════╩═ + 1 + +Join the circuits +================= + +You can join the circuits together using these two methods: + +1. Using the :meth:`~.QuantumCircuit.compose` method +2. Using the :meth:`~.QuantumCircuit.append` method + +.. warning:: + + The ``combine`` and ``extend`` methods have been deprecated in Qiskit Terra 0.17 and removed in 0.23. These methods are replaced by the :meth:`~.QuantumCircuit.compose` method which is more powerful. The removal of ``extend`` also means that the ``+`` and ``+=`` operators are no longer defined for :class:`~.QuantumCircuit`. Instead, you can use the ``&`` and ``&=`` operators respectively, which use :meth:`~.QuantumCircuit.compose`. + +For both methods, if the two circuits being combined have different sizes, the method needs to be called in the circuit that is bigger (more qubits and more classical bits). + +Using the :meth:`~.QuantumCircuit.compose` method +------------------------------------------------- + +In order to join two circuits with :meth:`~.QuantumCircuit.compose`, you only have to specify the circuit you want to insert. That way the qubits and bits of the smaller circuit will be included into the first qubits and bits of the bigger one in the original order they had. + +By default, :meth:`~.QuantumCircuit.compose` does not modify the original circuit to which it is applied but returns a new joint circuit object. This can be changed by setting the ``inplace`` argument to ``True``. + +.. testcode:: + + qc3 = qc2.compose(qc1) + print(qc3.draw()) + +.. testoutput:: + :options: +NORMALIZE_WHITESPACE + + ┌───┐ ┌───┐ + q_0: ┤ Y ├──■──┤ H ├──■───── + ├───┤ │ └───┘┌─┴─┐┌─┐ + q_1: ┤ X ├──┼───────┤ X ├┤M├ + └───┘ │ └───┘└╥┘ + q_2: ───────┼─────────────╫─ + ┌─┴─┐ ┌─┐ ║ + q_3: ─────┤ X ├─┤M├───────╫─ + └───┘ └╥┘ ║ + c: 2/════════════╩════════╩═ + 1 0 + +If you want to insert the qubits and bits into specific positions in the bigger circuit, you can use the ``qubits`` and ``bits`` arguments. The following example joins the two circuits by connecting `q_0` and `q_1` qubits of `qc1` circuit to `q_3` and `q_1` qubits of `qc2` circuit. + +.. testcode:: + + qc4 = qc2.compose(qc1, qubits=[3,1], clbits=[1]) + print(qc4.draw()) + +.. testoutput:: + :options: +NORMALIZE_WHITESPACE + + ┌───┐ + q_0: ┤ Y ├──■────────────────── + ├───┤ │ ┌───┐┌─┐ + q_1: ┤ X ├──┼──────────┤ X ├┤M├ + └───┘ │ └─┬─┘└╥┘ + q_2: ───────┼────────────┼───╫─ + ┌─┴─┐┌─┐┌───┐ │ ║ + q_3: ─────┤ X ├┤M├┤ H ├──■───╫─ + └───┘└╥┘└───┘ ║ + c: 2/═══════════╩════════════╩═ + 1 1 + +You can also join the smaller circuit in front of the bigger circuit by setting the ``front`` argument to ``True``. + +.. testcode:: + + qc5 = qc2.compose(qc1, front=True) + print(qc5.draw()) + +.. testoutput:: + :options: +NORMALIZE_WHITESPACE + + ┌───┐ ┌───┐ + q_0: ┤ H ├──■──┤ Y ├───────■───── + └───┘┌─┴─┐└┬─┬┘┌───┐ │ + q_1: ─────┤ X ├─┤M├─┤ X ├──┼───── + └───┘ └╥┘ └───┘ │ + q_2: ────────────╫─────────┼───── + ║ ┌─┴─┐┌─┐ + q_3: ────────────╫───────┤ X ├┤M├ + ║ └───┘└╥┘ + c: 2/════════════╩═════════════╩═ + 0 1 + +Using the :meth:`~.QuantumCircuit.append` method +------------------------------------------------ + +In order to join two circuits with :meth:`~.QuantumCircuit.append`, you need to specify the circuit you want to add, as well as the qubits and classical bits (if there are any) onto which you want the circuit to be applied. + +Different from :meth:`~.QuantumCircuit.compose`, this method modifies the circuit it is applied to, instead of returning a new circuit. + +.. testcode:: + + qc2.append(qc1, qargs=[3,1], cargs=[1]) + qc2.draw(cregbundle=False) + +.. code-block:: text + + ┌───┐ + q_0: ┤ Y ├──■───────────────────── + ├───┤ │ ┌──────────────┐ + q_1: ┤ X ├──┼─────┤1 ├ + └───┘ │ │ │ + q_2: ───────┼─────┤ ├ + ┌─┴─┐┌─┐│ │ + q_3: ─────┤ X ├┤M├┤0 circuit-101 ├ + └───┘└╥┘│ │ + c_0: ═══════════╬═╡ ╞ + ║ │ │ + c_1: ═══════════╩═╡0 ╞ + └──────────────┘ + +Unlike :meth:`~.QuantumCircuit.compose`, :meth:`~.QuantumCircuit.append` turns the smaller circuit into a single :class:`~qiskit.circuit.Instruction`. If you prefer joining the circuits using the individual gates, you can use :meth:`~.QuantumCircuit.decompose` to decompose the joint circuit. + +.. testcode:: + + print(qc2.decompose().draw()) + +.. testoutput:: + :options: +NORMALIZE_WHITESPACE + + ┌───────────────┐ + q_0: ┤ U3(π,π/2,π/2) ├──■────────────────── + └─┬───────────┬─┘ │ ┌───┐┌─┐ + q_1: ──┤ U3(π,0,π) ├────┼──────────┤ X ├┤M├ + └───────────┘ │ └─┬─┘└╥┘ + q_2: ───────────────────┼────────────┼───╫─ + ┌─┴─┐┌─┐┌───┐ │ ║ + q_3: ─────────────────┤ X ├┤M├┤ H ├──■───╫─ + └───┘└╥┘└───┘ ║ + c: 2/═══════════════════════╩════════════╩═ + 1 1 diff --git a/docs/how_to/create_a_parameterized_circuit.rst b/docs/how_to/create_a_parameterized_circuit.rst new file mode 100644 index 000000000000..8b5055f845db --- /dev/null +++ b/docs/how_to/create_a_parameterized_circuit.rst @@ -0,0 +1,114 @@ +###################################### +Create a parameterized quantum circuit +###################################### + +This guide will show how to create a :class:`~.QuantumCircuit` that includes parameters. + +Define the parameters +===================== + +In order to define a parameter, you need to create a :class:`~.Parameter` object. To do that you only need to choose a ``name``, which can be any unicode string like ``'θ'``. + +.. testcode:: + + from qiskit.circuit import Parameter + + theta = Parameter('θ') + + +Create the parameterized circuit +================================ + +When creating the circuit you can include the :class:`~.Parameter` you just created the same as if it was a defined number. + +.. testcode:: + + from qiskit import QuantumCircuit + + qc = QuantumCircuit(1) + qc.rx(theta, 0) + print(qc.draw()) + +.. testoutput:: + :options: +NORMALIZE_WHITESPACE + + ┌───────┐ + q: ┤ Rx(θ) ├ + └───────┘ + + +Assign values to parameters +=========================== + +You can use these two methods to assign values to the :class:`~.Parameter`\ s in your circuit: + +* :meth:`~.QuantumCircuit.bind_parameters` +* :meth:`~.QuantumCircuit.assign_parameters` + +:meth:`~.QuantumCircuit.bind_parameters` +-------------------------------------------------------- + +In order to use this method, you have to specify either a dictionary of the form ``{parameter: value,...}`` or an iterable formed only by numeric values. If using only numeric values they will be bound to their corresponding parameters in according to the order in :attr:`~.QuantumCircuit.parameters`. + +.. testcode:: + + import numpy as np + + theta_values = [0, np.pi/2, np.pi] + qc_bind_list = [qc.bind_parameters({theta: theta_value}) for theta_value in theta_values] + + for i in range(3): + print(qc_bind_list[i].draw()) + +.. testoutput:: + :options: +NORMALIZE_WHITESPACE + + ┌───────┐ + q: ┤ Rx(0) ├ + └───────┘ + ┌─────────┐ + q: ┤ Rx(π/2) ├ + └─────────┘ + ┌───────┐ + q: ┤ Rx(π) ├ + └───────┘ + +:meth:`~.QuantumCircuit.assign_parameters` +---------------------------------------------------------- + +This method works identically like :meth:`~.QuantumCircuit.bind_parameters` except that you can also assign other :class:`~.Parameter` objects instead of only numbers to the :class:`~.Parameter`\ s in your circuit. + +.. testcode:: + + phi = Parameter('ϕ') + + theta_values = [np.pi/2, phi] + qc_assign_list = [qc.assign_parameters({theta: theta_value}) for theta_value in theta_values] + + for i in range(2): + print(qc_assign_list[i].draw()) + +.. testoutput:: + :options: +NORMALIZE_WHITESPACE + + ┌─────────┐ + q: ┤ Rx(π/2) ├ + └─────────┘ + ┌───────┐ + q: ┤ Rx(ϕ) ├ + └───────┘ + + +Another difference between :meth:`~.QuantumCircuit.bind_parameters` and :meth:`~.QuantumCircuit.assign_parameters` is that for the latter, you can make it change your original circuit instead of creating a new one by setting the ``inplace`` argument to ``True``. + +.. testcode:: + + qc.assign_parameters({theta: np.pi/4}, inplace=True) + print(qc.draw()) + +.. testoutput:: + :options: +NORMALIZE_WHITESPACE + + ┌─────────┐ + q: ┤ Rx(π/4) ├ + └─────────┘ \ No newline at end of file diff --git a/docs/how_to/create_a_quantum_circuit.rst b/docs/how_to/create_a_quantum_circuit.rst new file mode 100644 index 000000000000..ca8b27f8760e --- /dev/null +++ b/docs/how_to/create_a_quantum_circuit.rst @@ -0,0 +1,169 @@ + +######################## +Create a quantum circuit +######################## + +This guide shows you how to create a quantum circuit. + +There are two ways to create a :class:`~.QuantumCircuit` object: + +1. Initialize :class:`~.QuantumCircuit` by directly specifying the number of qubits and classical bits you want. +2. Creating :class:`~.QuantumRegister`\ s and :class:`~.ClassicalRegister`\ s and use the registers to initialize a :class:`~.QuantumCircuit` + +.. _create circuit with qubit numbers: + +Create by specifying the number of qubits and classical bits +============================================================ + +You can create a :class:`~.QuantumCircuit` by only specifying the number of qubits and classical bits. For example: + +.. testcode:: + + from qiskit import QuantumCircuit + + # Initialize number of qubits and classical bits + n_qubits = 3 + n_bits = 2 + + # Create the circuit + qc = QuantumCircuit(n_qubits, n_bits) + print(qc.draw()) + +.. testoutput:: + :options: +NORMALIZE_WHITESPACE + + q_0: + + q_1: + + q_2: + + c: 2/ + + +You can also create a :class:`~.QuantumCircuit` with only qubits and no classical bits, by omitting the number of classical bits: + +.. testcode:: + + from qiskit import QuantumCircuit + + qc = QuantumCircuit(n_qubits) + print(qc.draw()) + +.. testoutput:: + :options: +NORMALIZE_WHITESPACE + + q_0: + + q_1: + + q_2: + +Create from quantum and classical registers +=========================================== + +Create quantum registers +------------------------ + +You can create a :class:`~.QuantumRegister` object by passing the desired number of qubits as an argument: + +.. testcode:: + + from qiskit import QuantumRegister + + # Create a quantum register with 2 qubits + qr1 = QuantumRegister(2) + + # Create a quantum register with 3 qubits + qr2 = QuantumRegister(3) + +Create classical registers +-------------------------- + +Similar to the quantum registers, you can create a :class:`~.ClassicalRegister` object by passing the desired number of classical bits as an argument: + +.. testcode:: + + from qiskit import ClassicalRegister + + # Create a classical register with 2 classical bits + cr1 = ClassicalRegister(2) + + # Create a classical register with 1 classical bit + cr2 = ClassicalRegister(1) + +Create a quantum circuit +------------------------ + +Now that you have defined the quantum and classical registers, you can create a :class:`~.QuantumCircuit` with the registers: + +.. testcode:: + + # Create the quantum circuit from the registers + qc = QuantumCircuit(qr1, qr2, cr1, cr2) + print(qc.draw()) + +.. testoutput:: + :options: +NORMALIZE_WHITESPACE + + q0_0: + + q0_1: + + q1_0: + + q1_1: + + q1_2: + + c0: 2/ + + c1: 1/ + + +You can put the registers in any order, even mixing classical and quantum. However, the relative order of the :class:`~.QuantumRegister`\ s affects the order of the qubits in the final circuit. The qubits from the first :class:`~.QuantumRegister` will be the first and so on. The same applies to the :class:`~.ClassicalRegister`\ s. + +.. testcode:: + + # Resulting quantum circuits will be the same if the quantum and classical registers have the same relative order + qc1 = QuantumCircuit(qr1, cr1, qr2, cr2) + + print(qc == qc1) + +.. testoutput:: + + True + +.. testcode:: + + # Resulting quantum circuits are different if the quantum or classical registers have different relative order + qc2 = QuantumCircuit(qr2, qr1, cr1, cr2) + + print(qc == qc2) + +.. testoutput:: + + False + + +.. testcode:: + + print(qc2.draw()) + +.. testoutput:: + :options: +NORMALIZE_WHITESPACE + + q1_0: + + q1_1: + + q1_2: + + q0_0: + + q0_1: + + c0: 2/ + + c1: 1/ + diff --git a/docs/how_to/index.rst b/docs/how_to/index.rst index a20d20870d99..ab97b2df48ed 100644 --- a/docs/how_to/index.rst +++ b/docs/how_to/index.rst @@ -4,6 +4,16 @@ How-to Guides ############# +Create a quantum circuit +======================== + +.. toctree:: + :maxdepth: 1 + + create_a_quantum_circuit + compose_quantum_circuits + visualize_a_quantum_circuit + create_a_parameterized_circuit Use the primitives ================== diff --git a/docs/how_to/use_estimator.rst b/docs/how_to/use_estimator.rst index 61a69ab5ffbc..dc7355efc1c8 100644 --- a/docs/how_to/use_estimator.rst +++ b/docs/how_to/use_estimator.rst @@ -36,6 +36,7 @@ Initialize quantum circuit ========================== Then you need to create the :class:`~qiskit.circuit.QuantumCircuit`\ s for which you want to obtain the expected value. +For more details about this part check out :doc:`this guide `. .. plot:: :include-source: @@ -66,7 +67,7 @@ Then you need to create the :class:`~qiskit.circuit.QuantumCircuit`\ s for which Initialize the ``Estimator`` ============================ -Then, you need to instantiate an :class:`~qiskit.primitives.Estimator`. +Next, you need to instantiate an :class:`~qiskit.primitives.Estimator`. .. testcode:: @@ -112,11 +113,11 @@ whose ``i``-th element is the expectation value corresponding to the ``i``-th ci 3.999999999999999 -Parameterized circuit with ``Estimator`` -======================================== +Parameterized circuits with ``Estimator`` +========================================= -The :class:`~qiskit.primitives.Estimator` primitive can be run with unbound parameterized circuits like the one below. -You can also manually bind values to the parameters of the circuit and follow the steps +The :class:`~qiskit.primitives.Estimator` primitive also has the option to include unbound parameterized circuits like the one below. +You can also bind values to the parameters of the circuit like in :doc:`this guide ` and follow the steps of the previous example. .. testcode:: @@ -137,9 +138,9 @@ of the previous example. q_1: ─────────┤ X ├ └───┘ -The main difference with the previous case is that now you need to specify the sets of parameter values +The main difference between this example and the previous one is that now you need to include the parameter values for which you want to evaluate the expectation value as a ``list`` of ``list``\ s of ``float``\ s. -The ``i``-th element of the outer``list`` is the set of parameter values +The idea is that the ``i``-th element of the bigger ``list`` is the set of parameter values that corresponds to the ``i``-th circuit and observable. .. testcode:: @@ -266,4 +267,4 @@ And then you can introduce it into your new :class:`~qiskit.primitives.Estimator .. testoutput:: - 3.999999998697238 \ No newline at end of file + 3.999999998697238 diff --git a/docs/how_to/use_sampler.rst b/docs/how_to/use_sampler.rst index da7257c28fa1..e33255630fa5 100644 --- a/docs/how_to/use_sampler.rst +++ b/docs/how_to/use_sampler.rst @@ -50,12 +50,12 @@ The first step is to create the :class:`~qiskit.circuit.QuantumCircuit`\ s from .. note:: - The :class:`~qiskit.circuit.QuantumCircuit` you pass to :class:`~qiskit.primitives.Sampler` has to include measurements. + The :class:`~qiskit.circuit.QuantumCircuit` you pass to :class:`~qiskit.primitives.Sampler` must include measurements. Initialize the ``Sampler`` ========================== -Then, you need to create a :class:`~qiskit.primitives.Sampler` instance. +Next, you need to create a :class:`~qiskit.primitives.Sampler` instance. .. testcode:: @@ -63,12 +63,11 @@ Then, you need to create a :class:`~qiskit.primitives.Sampler` instance. sampler = Sampler() -Run and get results -=================== +Run job and get results +======================= -Now that you have defined your ``sampler``, you can run it by calling the :meth:`~qiskit.primitives.Sampler.run` method, -which returns an instance of :class:`~.PrimitiveJob` (subclass of :class:`~qiskit.providers.JobV1`). You can get the results from the job (as a :class:`~qiskit.primitives.SamplerResult` object) -with the :meth:`~qiskit.providers.JobV1.result` method. +Now that you have defined ``sampler``, you can create a :class:`~.PrimitiveJob` (subclass of :class:`~qiskit.providers.JobV1`) with the +:meth:`~qiskit.primitives.Sampler.run` method and then you can get the results (as a :class:`~qiskit.primitives.SamplerResult` object) with the :meth:`~qiskit.providers.JobV1.result` method. .. testcode:: @@ -109,7 +108,8 @@ Even though there is only one circuit in this example, :attr:`~qiskit.primitives Probability distribution with binary outputs ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -If you prefer to see the output keys as binary strings instead of decimal numbers, you can use the +If you prefer to see the outputs as binary strings instead of decimals, you can use the + :meth:`~qiskit.result.QuasiDistribution.binary_probabilities` method. .. testcode:: @@ -120,11 +120,12 @@ If you prefer to see the output keys as binary strings instead of decimal number {'00': 0.4999999999999999, '11': 0.4999999999999999} -Parameterized circuit with ``Sampler`` -======================================== -The :class:`~qiskit.primitives.Sampler` primitive can be run with unbound parameterized circuits like the one below. -You can also manually bind values to the parameters of the circuit and follow the steps +Parameterized circuits with ``Sampler`` +========================================= + +The :class:`~qiskit.primitives.Sampler` primitive also has the option to include unbound parameterized circuits like the one below. +You can also bind values to the parameters of the circuit like in :doc:`this guide ` and follow the steps of the previous example. .. testcode:: @@ -148,10 +149,9 @@ of the previous example. meas: 2/══════════════════╩══╩═ 0 1 -The main difference from the previous case is that now you need to specify the sets of parameter values -for which you want to evaluate the expectation value as a ``list`` of ``list``\ s of ``float``\ s. -The ``i``-th element of the outer ``list`` is the set of parameter values -that corresponds to the ``i``-th circuit. +The main difference from the previous case is that now you need to include the parameter values +for which you want to sample the probabilities as a ``list`` of ``list``\ s of ``float``\ s. +The idea is that the ``i``-th element of the bigger ``list`` is the set of parameter values .. testcode:: @@ -252,4 +252,4 @@ And then you can introduce it into your new :class:`~qiskit.primitives.Sampler` .. testoutput:: - SamplerResult(quasi_dists=[{0: 0.5205078125, 3: 0.4794921875}], metadata=[{'shots': 2048}]) \ No newline at end of file + SamplerResult(quasi_dists=[{0: 0.5205078125, 3: 0.4794921875}], metadata=[{'shots': 2048}]) diff --git a/docs/how_to/visualize_a_quantum_circuit.rst b/docs/how_to/visualize_a_quantum_circuit.rst new file mode 100644 index 000000000000..81306d75e8c6 --- /dev/null +++ b/docs/how_to/visualize_a_quantum_circuit.rst @@ -0,0 +1,133 @@ +########################### +Visualize a quantum circuit +########################### + +This guide shows you how to visualize a quantum circuit. + +Create a circuit +================= + +Let's first create a quantum circuit for the visualization. + +.. testcode:: + + from qiskit import QuantumCircuit + + qc = QuantumCircuit(3, 3) + qc.h(range(3)) + qc.cx(0, 1) + qc.measure(range(3), range(3)) + +Visualize the circuit +===================== + +You can visualize a quantum circuit in the following ways: + +1. Using the ``print()`` function. +2. Using the :meth:`~.QuantumCircuit.draw` method. +3. Using the :func:`~.circuit_drawer` function. + +Using the ``print()`` function +------------------------------ + +If you call the ``print()`` function on a :class:`~.QuantumCircuit` object, you will get an `ASCII art version `_ of the circuit diagram. + +.. testcode:: + + print(qc) + +.. testoutput:: + :options: +NORMALIZE_WHITESPACE + + ┌───┐ ┌─┐ + q_0: ┤ H ├──■──┤M├─── + ├───┤┌─┴─┐└╥┘┌─┐ + q_1: ┤ H ├┤ X ├─╫─┤M├ + ├───┤└┬─┬┘ ║ └╥┘ + q_2: ┤ H ├─┤M├──╫──╫─ + └───┘ └╥┘ ║ ║ + c: 3/═══════╩═══╩══╩═ + 2 0 1 + +Using the :meth:`~.QuantumCircuit.draw` method +---------------------------------------------- + +You can also use the :meth:`.QuantumCircuit.draw` method to visualize it. The default output style is 'text', which will output an ASCII art version, the same as using the ``print()`` function. + +.. code-block:: python + + qc.draw() + +.. code-block:: text + + ┌───┐ ┌─┐ + q_0: ┤ H ├──■──┤M├─── + ├───┤┌─┴─┐└╥┘┌─┐ + q_1: ┤ H ├┤ X ├─╫─┤M├ + ├───┤└┬─┬┘ ║ └╥┘ + q_2: ┤ H ├─┤M├──╫──╫─ + └───┘ └╥┘ ║ ║ + c: 3/═══════╩═══╩══╩═ + 2 0 1 + +You can also change the output style using the ``output`` argument. These are the available output styles: + +1. ``'text'``: renders the circuit with ASCII art. It's the default option. +2. ``'mpl'``: uses `matplotlib `_ to render the circuit. +3. ``'latex'``: uses :math:`\LaTeX` to render the circuit. It requires a full `LaTeX `_ distribution and the package ``pdflatex``. +4. ``'latex_source'``: outputs the :math:`\LaTeX` source code that creates the ``'latex'`` rendering of the circuit. + +Because this optional keyword argument is the first of this method, you can type ``qc.draw(option)`` instead of ``qc.draw(output=option)`` + + +``'mpl'`` ouptut +^^^^^^^^^^^^^^^^ + +.. code-block:: python + + qc.draw('mpl') + +.. plot:: + + from qiskit import QuantumCircuit + + qc = QuantumCircuit(3, 3) + qc.h(range(3)) + qc.cx(0, 1) + qc.measure(range(3), range(3)) + qc.draw("mpl") + + +``'latex_source'`` output +^^^^^^^^^^^^^^^^^^^^^^^^^ + +.. code-block:: python + + qc.draw('latex_source') + +.. code-block:: text + + '\\documentclass[border=2px]{standalone}\n\n\\usepackage[braket, qm]{qcircuit}\n\\usepackage{graphicx}\n\n\\begin{document}\n\\scalebox{1.0}{\n\\Qcircuit @C=1.0em @R=0.2em @!R { \\\\\n\t \t\\nghost{{q}_{0} : } & \\lstick{{q}_{0} : } & \\gate{\\mathrm{H}} & \\ctrl{1} & \\meter & \\qw & \\qw & \\qw\\\\\n\t \t\\nghost{{q}_{1} : } & \\lstick{{q}_{1} : } & \\gate{\\mathrm{H}} & \\targ & \\qw & \\meter & \\qw & \\qw\\\\\n\t \t\\nghost{{q}_{2} : } & \\lstick{{q}_{2} : } & \\gate{\\mathrm{H}} & \\meter & \\qw & \\qw & \\qw & \\qw\\\\\n\t \t\\nghost{\\mathrm{{c} : }} & \\lstick{\\mathrm{{c} : }} & \\lstick{/_{_{3}}} \\cw & \\dstick{_{_{\\hspace{0.0em}2}}} \\cw \\ar @{<=} [-1,0] & \\dstick{_{_{\\hspace{0.0em}0}}} \\cw \\ar @{<=} [-3,0] & \\dstick{_{_{\\hspace{0.0em}1}}} \\cw \\ar @{<=} [-2,0] & \\cw & \\cw\\\\\n\\\\ }}\n\\end{document}' + + +Using the :func:`~.circuit_drawer` function +------------------------------------------- + +If you prefer to use a self-contained function instead of a :class:`~.QuantumCircuit` method to draw your circuit, you can do it with :func:`~.circuit_drawer` from :mod:`qiskit.visualization`. It has the exact same behavior as the :meth:`~.QuantumCircuit.draw` method above, except that it requires the circuit to be included as an argument. + +.. code-block:: python + + from qiskit.visualization import circuit_drawer + + circuit_drawer(qc, output='mpl') + +.. plot:: + + from qiskit import QuantumCircuit + from qiskit.visualization import circuit_drawer + + qc = QuantumCircuit(3, 3) + qc.h(range(3)) + qc.cx(0, 1) + qc.measure(range(3), range(3)) + circuit_drawer(qc, output='mpl') \ No newline at end of file