In [1]:
import qiskit
from qiskit.providers.fake_provider import FakeHanoi
In [2]:
circuit = qiskit.QuantumCircuit(2)

circuit.h(0)  #Hadamard Gate: qubit has equal probablity of |0> or |1>
circuit.h(1)  #Hadamard Gate: qubit has equal probablity of |0> or |1>
circuit.measure_all()
In [3]:
circuit.draw(output="mpl")
Out[3]:
No description has been provided for this image
In [4]:
backend = FakeHanoi()
job_sim = qiskit.execute(circuit, backend, shots=1000000)
job_sim.result().get_counts(circuit)
Out[4]:
{'10': 250160, '11': 249015, '00': 251319, '01': 249506}
In [5]:
# 1999,2054,1966,1981
# 2101,1965,1965,1969
# 1974,2000,2002,2024
# 2057,1897,2046,2000
# 1999,1987,1937,2077

# 2466,2454,2552,2528

# 24999,25112,24944,24945

# 249404,251101,250489,249006
# 250160,249015,251319,249506
In [7]:
# Create a circuit with a register of three qubits
circ = qiskit.QuantumCircuit(3)
# H gate on qubit 0, putting this qubit in a superposition of |0> + |1>.
circ.h(0)
# A CX (CNOT) gate on control qubit 0 and target qubit 1 generating a Bell state.
circ.cx(0, 1)
# 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('mpl')
Out[7]:
No description has been provided for this image
In [9]:
qc = qiskit.QuantumCircuit(5)
qc.h(0)
qc.cx(0, range(1, 5))
qc.measure_all()

qc.draw('mpl')
Out[9]:
No description has been provided for this image
In [11]:
from qiskit import QuantumRegister, ClassicalRegister

qr  = QuantumRegister(3, 'q')
anc = QuantumRegister(1, 'ancilla')
cr  = ClassicalRegister(3, 'c')
qc  = qiskit.QuantumCircuit(qr, anc, cr)

qc.x(anc[0])
qc.h(anc[0])
qc.h(qr[0:3])
qc.cx(qr[0:3], anc[0])
qc.h(qr[0:3])
qc.barrier(qr)
qc.measure(qr, cr)

qc.draw('mpl')
Out[11]:
No description has been provided for this image
In [13]:
from qiskit.circuit import Parameter

a, b, elephant = Parameter("a"), Parameter("b"), Parameter("elephant")

circuit = qiskit.QuantumCircuit(1)
circuit.rx(b, 0)
circuit.rz(elephant, 0)
circuit.ry(a, 0)
circuit.parameters  # sorted alphabetically!

circuit.draw('mpl')
Out[13]:
No description has been provided for this image
In [17]:
from qiskit.circuit import ParameterVector
x = ParameterVector("x", 12)
circuit = qiskit.QuantumCircuit(1)
for x_i in x:
    circuit.rx(x_i, 0)
circuit.parameters
Out[17]:
ParameterView([ParameterVectorElement(x[0]), ParameterVectorElement(x[1]), ParameterVectorElement(x[2]), ParameterVectorElement(x[3]), ParameterVectorElement(x[4]), ParameterVectorElement(x[5]), ParameterVectorElement(x[6]), ParameterVectorElement(x[7]), ParameterVectorElement(x[8]), ParameterVectorElement(x[9]), ParameterVectorElement(x[10]), ParameterVectorElement(x[11])])
In [18]:
circuit.draw('mpl')
Out[18]:
No description has been provided for this image
In [ ]:
#https://qiskit.org/documentation/tutorials/circuits_advanced/03_advanced_circuit_visualization.html
In [20]:
# Build a quantum circuit
circuit = qiskit.QuantumCircuit(3, 3)

circuit.x(1)
circuit.h(range(3))
circuit.cx(0, 1)
circuit.measure(range(3), range(3));

print(circuit)
     ┌───┐          ┌─┐   
q_0: ┤ H ├───────■──┤M├───
     ├───┤┌───┐┌─┴─┐└╥┘┌─┐
q_1: ┤ X ├┤ H ├┤ X ├─╫─┤M├
     ├───┤└┬─┬┘└───┘ ║ └╥┘
q_2: ┤ H ├─┤M├───────╫──╫─
     └───┘ └╥┘       ║  ║ 
c: 3/═══════╩════════╩══╩═
            2        0  1 
In [21]:
circuit.draw()
Out[21]:
     ┌───┐          ┌─┐   
q_0: ┤ H ├───────■──┤M├───
     ├───┤┌───┐┌─┴─┐└╥┘┌─┐
q_1: ┤ X ├┤ H ├┤ X ├─╫─┤M├
     ├───┤└┬─┬┘└───┘ ║ └╥┘
q_2: ┤ H ├─┤M├───────╫──╫─
     └───┘ └╥┘       ║  ║ 
c: 3/═══════╩════════╩══╩═
            2        0  1 
In [22]:
# Matplotlib Drawing
circuit.draw(output='mpl')
Out[22]:
No description has been provided for this image
In [23]:
# Draw a new circuit with barriers and more registers

q_a = QuantumRegister(3, name='qa')
q_b = QuantumRegister(5, name='qb')
c_a = ClassicalRegister(3)
c_b = ClassicalRegister(5)

circuit = qiskit.QuantumCircuit(q_a, q_b, c_a, c_b)

circuit.x(q_a[1])
circuit.x(q_b[1])
circuit.x(q_b[2])
circuit.x(q_b[4])
circuit.barrier()
circuit.h(q_a)
circuit.barrier(q_a)
circuit.h(q_b)
circuit.cswap(q_b[0], q_b[1], q_b[2])
circuit.cswap(q_b[2], q_b[3], q_b[4])
circuit.cswap(q_b[3], q_b[4], q_b[0])
circuit.barrier(q_b)
circuit.measure(q_a, c_a)
circuit.measure(q_b, c_b);

# Draw the circuit
circuit.draw(output='mpl')
Out[23]:
No description has been provided for this image
In [24]:
# Draw the circuit with reversed bit order
circuit.draw(output='mpl', reverse_bits=True)
Out[24]:
No description has been provided for this image
In [26]:
#Draw the circuit without barriers
circuit.draw(output='mpl', plot_barriers=False)
Out[26]:
No description has been provided for this image
In [27]:
# Draw the circuit without barriers and reverse bit order
circuit.draw(output='mpl', plot_barriers=False, reverse_bits=True)
Out[27]:
No description has been provided for this image
In [28]:
# Set line length to 80 for above circuit
circuit.draw(output='text')
Out[28]:
            ░ ┌───┐ ░    ┌─┐                           
qa_0: ──────░─┤ H ├─░────┤M├───────────────────────────
      ┌───┐ ░ ├───┤ ░    └╥┘┌─┐                        
qa_1: ┤ X ├─░─┤ H ├─░─────╫─┤M├────────────────────────
      └───┘ ░ ├───┤ ░     ║ └╥┘┌─┐                     
qa_2: ──────░─┤ H ├─░─────╫──╫─┤M├─────────────────────
            ░ ├───┤ ░     ║  ║ └╥┘    ░ ┌─┐            
qb_0: ──────░─┤ H ├─■─────╫──╫──╫──X──░─┤M├────────────
      ┌───┐ ░ ├───┤ │     ║  ║  ║  │  ░ └╥┘┌─┐         
qb_1: ┤ X ├─░─┤ H ├─X─────╫──╫──╫──┼──░──╫─┤M├─────────
      ├───┤ ░ ├───┤ │     ║  ║  ║  │  ░  ║ └╥┘┌─┐      
qb_2: ┤ X ├─░─┤ H ├─X──■──╫──╫──╫──┼──░──╫──╫─┤M├──────
      └───┘ ░ ├───┤    │  ║  ║  ║  │  ░  ║  ║ └╥┘┌─┐   
qb_3: ──────░─┤ H ├────X──╫──╫──╫──■──░──╫──╫──╫─┤M├───
      ┌───┐ ░ ├───┤    │  ║  ║  ║  │  ░  ║  ║  ║ └╥┘┌─┐
qb_4: ┤ X ├─░─┤ H ├────X──╫──╫──╫──X──░──╫──╫──╫──╫─┤M├
      └───┘ ░ └───┘       ║  ║  ║     ░  ║  ║  ║  ║ └╥┘
c0: 3/════════════════════╩══╩══╩════════╬══╬══╬══╬══╬═
                          0  1  2        ║  ║  ║  ║  ║ 
c1: 5/═══════════════════════════════════╩══╩══╩══╩══╩═
                                         0  1  2  3  4 
In [29]:
# Change the background color in mpl

style = {'backgroundcolor': 'lightgreen'}

circuit.draw(output='mpl', style=style)
Out[29]:
No description has been provided for this image
In [30]:
# Scale the mpl output to 1/2 the normal size
circuit.draw(output='mpl', scale=0.5)
Out[30]:
No description has been provided for this image
In [31]:
from qiskit.tools.visualization import circuit_drawer
circuit_drawer(circuit, output='mpl', plot_barriers=False)
Out[31]:
No description has been provided for this image
In [32]:
import qiskit.tools.jupyter
%qiskit_version_table
%qiskit_copyright

Version Information

Qiskit SoftwareVersion
qiskit-terra0.24.1
qiskit-aer0.12.0
qiskit-ibmq-provider0.20.2
qiskit0.43.1
System information
Python version3.10.9
Python compilerMSC v.1916 64 bit (AMD64)
Python buildmain, Mar 1 2023 18:18:15
OSWindows
CPUs4
Memory (Gb)31.963764190673828
Sat Jun 24 23:16:02 2023 Central Daylight Time

This code is a part of Qiskit

© Copyright IBM 2017, 2023.

This code is licensed under the Apache License, Version 2.0. You may
obtain a copy of this license in the LICENSE.txt file in the root directory
of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.

Any modifications or derivative works of this code must retain this
copyright notice, and modified files need to carry a notice indicating
that they have been altered from the originals.

In [ ]: