Quantum Computing Asked by user727041 on July 24, 2021
Qiskit Aqua has a module that implements the HHL algorithm:
https://qiskit.org/documentation/_modules/qiskit/aqua/algorithms/single_sample/hhl/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.
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)
Correct answer by met927 on July 24, 2021
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.
Answered by user727041 on July 24, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP