0
$\begingroup$

I am trying to implement a Hadamard test on a parametrized sub-circuit $V(\bf{a})$. I create an outer circuit and then append the parametrized $V(\bf{a})$ to it.

def outer_circuit(subcircuit, imaginary=False):

    qc = QuantumCircuit(4, 1)
    qc.append(subcircuit, qc.qubits)
    [...] # operations 
    
    return qc


def subcircuit():
    
    a = ParameterVector('a', 9)
    m, n = Parameter('m'), Parameter('n')
    qc = QuantumCircuit(4, name='V')

    [...] # random operations which use parameters

    return qc

When I finally contruct the bigger circuit and try to bind the parameters, I get the following error:


operator = subcircuit()

test = outer_circuit(operator)

test.bind_parameters({'a': np.random.rand(9)})

>>
CircuitError: 'Cannot bind parameters (a) not present in the circuit.'

How do I get overcome this problem?

$\endgroup$

1 Answer 1

0
$\begingroup$

When you append a sub-circuit to a larger circuit, the sub-circuit parameters are added to list of circuit parameters. You can easily check this as follows:

operator = subcircuit()
print(operator.parameters)

test = outer_circuit(operator)
print(test.parameters)

So, you can bind them as any parameters in the larger circuit.

The issue is that the keys of the parameter dictionary must be the Parameter instances not the Parameter names[see here]. So, you need to call bind_parameters as follows:

test.bind_parameters({a: np.random.rand(9)})

Of course to do this the variable a needs to be in the scope when you call bind_parameters. The fastest way to do this in your case - although not recommended - is to make it global:

def subcircuit():
    global a
    a = ParameterVector('a', 9)
    [...]
$\endgroup$
1
  • 1
    $\begingroup$ Yeah, making a global would solve the problem. Or just declaring a in main() and having subcircuit receiving it as input. $\endgroup$
    – consthatza
    Apr 1, 2022 at 11:36

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.