TransWikia.com

What is the best way to handle parametric circuits in Qiskit?

Quantum Computing Asked by Bidon on December 9, 2020

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?

One Answer

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...

Answered by Bidon on December 9, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP