6
$\begingroup$

Qiskit Aqua has a module that implements the HHL algorithm:

https://qiskit.org/documentation/stable/0.32/stubs/qiskit.aqua.algorithms.HHL.html

How does one draw the implemented circuit?

*** EDIT *** I'm working with an example from the github. I modified it as follows:

#
# Example from 
# https://github.com/Qiskit/qiskit-iqx-tutorials/blob/master/qiskit/advanced/aqua/linear_systems_of_equations.ipynb
#


from qiskit.aqua import run_algorithm
from qiskit.aqua.input import LinearSystemInput
from qiskit.quantum_info import state_fidelity
from qiskit.aqua.algorithms.classical import ExactLSsolver
import numpy as np


params = {
    'problem': {
        'name': 'linear_system'
    },
    'algorithm': {
        'name': 'HHL'
    },
    'eigs': {
        'expansion_mode': 'suzuki',
        'expansion_order': 2,
        'name': 'EigsQPE',
        'num_ancillae': 3,
        'num_time_slices': 50
    },
    'reciprocal': {
        'name': 'Lookup'
    },
    'backend': {
        'provider': 'qiskit.BasicAer',
        'name': 'statevector_simulator'
    }
}

# The 2 x 2 System to solve 
#


matrix = [[1, 0], [0, 2]]
vector = [1, 4]
params['input'] = {
    'name': 'LinearSystemInput',
    'matrix': matrix,
    'vector': vector
}


# Run the HHL Algorithm
result = run_algorithm(params)

result is the following:

{'probability_result': 0.024629684664855277,
 'output': array([0.46919178+0.00000000e+00j, 0.8830963 -6.70000184e-15j]),
 'solution': array([1.05859322-8.03148712e-15j, 1.99244701-3.02331663e-14j]),
 'matrix': array([[1, 0],
        [0, 2]]),
 'vector': array([1, 4]),
 'circuit_info': {'size': 28870,
  'depth': 28820,
  'width': 7,
  'qubits': 7,
  'bits': 0,
  'factors': 1,
  'operations': {'u3': 6011,
   'u1': 8021,
   'cx': 14066,
   'barrier': 700,
   'u2': 6,
   'cu3': 42,
   'x': 24}
  }
}

I tried the following

HHL.construct_circuit(result.circuit_info).draw()

but that resulted in

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-13-4cf1b322ed4c> in <module>
----> 1 HHL.construct_circuit(result.circuit_info).draw()

NameError: name 'HHL' is not defined

So it may very well be that I didn't import something or that I just don't understand the syntax for the HHL.

So how should I form the instructions to generate the circuit? (I'm not very good at python so if this question is trivial, I do apologize.) I checked the qiskit documentation but I couldn't understand it well. It didn't give an explicit example so if there's something I need to learn to be able to do this, please let me know. Thank you for your time.

$\endgroup$

2 Answers 2

6
$\begingroup$

You can draw the circuit using construct_circuit().draw().

In the tutorial you are talking about, if you scroll down to the 4x4 randomly generated section that uses params5 you can run print(hhl.construct_circuit()), after the line hhl = HHL.init_params(params5, algo_input).

This may take a little while to complete but it should eventually print out ASCII art of the circuit. Other drawing options can be configured using .draw().

NB this isn't to say that you can't draw any of the other circuits, if you follow the way the HHL object is created in this section you can draw any of the configurations specified by the params. For example, you could draw the circuit you are creating using

from qiskit.aqua.input import LinearSystemInput
from qiskit.aqua.algorithms.single_sample import HHL
algo_input = LinearSystemInput(matrix=matrix, vector=vector)
hhl = HHL.init_params(params, algo_input)
$\endgroup$
7
  • $\begingroup$ Please post the full code. I get errors saying register size must be positive when trying to use those methods. $\endgroup$ Nov 25, 2019 at 19:39
  • $\begingroup$ Thanks for the suggestion. I tried to make use of it previously but to no avail. I posted my code (it's a chopped up version from github). Please comment on what I should do. Thank you. $\endgroup$
    – user727041
    Nov 25, 2019 at 19:47
  • $\begingroup$ @user727041 I have updated my response, I hope this makes things clearer :) $\endgroup$
    – met927
    Nov 25, 2019 at 20:32
  • $\begingroup$ @met927 Thanks for updating your response. Out of curiosity, have you done this successfully? For a "fast" run, I'm trying to generate the circuit for the first $2 \times 2$ diagonal matrix and vector. I'm running this on a decent laptop, and it's been running for an hour and half so far (using about 4 GB of RAM and 20% CPU throughout). This seems a bit extreme for a small system of linear equations like this. $\endgroup$ Nov 26, 2019 at 0:18
  • 2
    $\begingroup$ @Chained Symmetry For the 2x2 case, try one without a negative eigenvalue and state so in the parameters. It ran generated the ascii circuit. $\endgroup$
    – user727041
    Nov 26, 2019 at 4:39
2
$\begingroup$

The credit for this answer goes to met927 in the previous post. So please upvote that answer instead of this one. met927's response answered my question. Not setting up some of the parameters to make system draw faster was my error. So thank you met927 for responding quickly and answering my question!

Below is a snippet that one can run quite quickly (less than a minute).

###########################################################
#
#
# met927's response incorporated into the github example
#
#
###########################################################


from qiskit.aqua import run_algorithm
from qiskit.aqua.input import LinearSystemInput
from qiskit.aqua.algorithms.single_sample import HHL

import numpy as np

# System to solve
# A  = [[1.5, 0.5], [0.5, 1.5]]
# b =  [2, 2]

matrix = [[1.5, 0.5], [0.5, 1.5]]
vector = [2, 2]

params = {
    'problem': {
        'name': 'linear_system'
    },
    'algorithm': {
        'name': 'HHL'
    },
    'eigs': {
        'expansion_mode': 'suzuki',
        'expansion_order': 2,
        'name': 'EigsQPE',
        'num_ancillae': 3,
        'num_time_slices': 50
    },
    'reciprocal': {
        'name': 'Lookup'
    },
    'backend': {
        'provider': 'qiskit.BasicAer',
        'name': 'statevector_simulator'
    }
}

params['input'] = {
    'name': 'LinearSystemInput',
    'matrix': matrix,
    'vector': vector
}

params['algorithm'] = {
    'truncate_powerdim': False,
    'truncate_hermitian': False
}
params['reciprocal'] = {
    'name': 'Lookup',
    'negative_evals': True
}
params['eigs'] = {
    'expansion_mode': 'suzuki',
    'expansion_order': 2,
    'name': 'EigsQPE',
    'negative_evals': False,
    'num_ancillae': 1,
    'num_time_slices': 70
}
params['initial_state'] = {
    'name': 'CUSTOM'
}
params['iqft'] = {
    'name': 'STANDARD'
}
params['qft'] = {
    'name': 'STANDARD'
}


# Prep the input
algo_input = LinearSystemInput(matrix=matrix, vector=vector)

# Run the HHL algorithm
hhl = HHL.init_params(params, algo_input)

# Draw the circuit
print(hhl.construct_circuit())

It draws an ascii circuit. I just wanted to know how to get the routine to draw the circuit. Thanks again met927.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.