# Function in Qiskit to get the quantum circuit

Is there any way to view the quantum circuit of pre implemented quantum algorithm in Qiskit? In the Qiskit textbook, there is an example given for HHL algorithm. Is there any function in Qiskit which lets us see the quantum circuit that was used to compute the results(probabilities) for this example?

The code for HHL example is given below:

from qiskit import Aer, transpile, assemble
from qiskit.circuit.library import QFT
from qiskit.aqua import QuantumInstance, aqua_globals
from qiskit.quantum_info import state_fidelity
from qiskit.aqua.algorithms import HHL, NumPyLSsolver
from qiskit.aqua.components.eigs import EigsQPE
from qiskit.aqua.components.reciprocals import LookupRotation
from qiskit.aqua.operators import MatrixOperator
from qiskit.aqua.components.initial_states import Custom
import numpy as np

def create_eigs(matrix, num_auxiliary, num_time_slices, negative_evals):
ne_qfts = [None, None]
if negative_evals:
num_auxiliary += 1
ne_qfts = [QFT(num_auxiliary - 1), QFT(num_auxiliary - 1).inverse()]

return EigsQPE(MatrixOperator(matrix=matrix),
QFT(num_auxiliary).inverse(),
num_time_slices=num_time_slices,
num_ancillae=num_auxiliary,
expansion_mode='suzuki',
expansion_order=2,
evo_time=None,  # This is t, can set to: np.pi*3/4
negative_evals=negative_evals,
ne_qfts=ne_qfts)

def fidelity(hhl, ref):
solution_hhl_normed = hhl / np.linalg.norm(hhl)
solution_ref_normed = ref / np.linalg.norm(ref)
fidelity = state_fidelity(solution_hhl_normed, solution_ref_normed)
print("Fidelity:\t\t %f" % fidelity)

matrix = [[1, -1/3], [-1/3, 1]]
vector = [1, 0]

orig_size = len(vector)
matrix, vector, truncate_powerdim, truncate_hermitian = HHL.matrix_resize(matrix, vector)

# Initialize eigenvalue finding module
eigs = create_eigs(matrix, 3, 50, False)
num_q, num_a = eigs.get_register_sizes()

# Initialize initial state module
init_state = Custom(num_q, state_vector=vector)

# Initialize reciprocal rotation module
reci = LookupRotation(negative_evals=eigs._negative_evals, evo_time=eigs._evo_time)

algo = HHL(matrix, vector, truncate_powerdim, truncate_hermitian, eigs,
init_state, reci, num_q, num_a, orig_size)

result = algo.run(QuantumInstance(Aer.get_backend('statevector_simulator')))
print("Solution:\t\t", np.round(result['solution'], 5))

result_ref = NumPyLSsolver(matrix, vector).run()
print("Classical Solution:\t", np.round(result_ref['solution'], 5))

print("Probability:\t\t %f" % result['probability_result'])
fidelity(result['solution'], result_ref['solution'])


The output is

Solution:            [1.13586-0.j 0.40896-0.j]
Classical Solution:  [1.125 0.375]
Probability:         0.056291
Fidelity:            0.999432


What you can do to see your circuit here is to use construct_circuit, see the documentation; notice it can take an optional parameter measurement (bool, indicate whether measurement on ancillary qubit should be performed).

To use it here, what you do is just this

my_hhl_circuit=algo.construct_circuit() #to stock it somewhere if you want
my_hhl_circuit.draw() #draw it


Don't hesitate to tell me if you need something else!

• The code given by you is working fine. I just wanted to add that when the above command is run, the circuit displayed has some black boxes like controlled evolution , qft , etc. Is it possible to decompose these black boxes also? Mar 23, 2021 at 17:54
• Yes you can, you just have to add the decompose function before the draw one, like this: circuit.decompose().draw(), tell me if this works!
– Lena
Mar 23, 2021 at 17:56
• Thanks, now everything is working fine. Mar 23, 2021 at 18:21