3
$\begingroup$

What is the best way to handle parametric circuits in Qiskit? When I create a variational form like TwoLocal and then do var_form.parameters it returns a set but that is an unordered structure so I fear that it will give me different results upon different calls.

Here is what I have:

var_form = TwoLocal(num_qubits=4,
                       rotation_blocks=["ry","rx"],
                       entanglement_blocks=["cx"],
                       entanglement="full",
                       reps=1,
                       skip_final_rotation_layer=False,
                       insert_barriers=True)

parameters=np.random.random(var_form.num_parameters)
param_dict={}
for i in range(var_form.num_parameters):
    param_dict[list(var_form.parameters)[i]]=parameters[i]

Here is what I want:

def get_var_form(parameters):
    """parameters is a list like [0,1,2,3,4....]"""
    
    ## 
    # DO MAGIC HERE 
    var_form = TwoLocal(num_qubits=4,
                       rotation_blocks=["ry","rx"],
                       entanglement_blocks=["cx"],
                       entanglement="full",
                       reps=1,
                       skip_final_rotation_layer=False,
                       insert_barriers=True)
    
    #I want to insert parameters----->var_form
    ##
    
    return var_form #with given parameters

How can I do this?

$\endgroup$
1
$\begingroup$

This is what I came up with:

param_dict={}
for i in range(var_form.num_parameters):
    param_dict[list(var_form.ordered_parameters)[i]]=parameters[i]

ansatz=var_form.bind_parameters(param_dict)

maybe its not the best way, but it works...

$\endgroup$
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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