I'm trying to implement a Parametric Hadamard Test.

I have already my parametric evolution gate $\exp(-i\theta H)$ where $\theta$ is the parameter.

When I defined the controlled-gate with Gate.control(1) and add it to the main circuit. I'm unable to correctly bind the parameters after and I can not get the state vector after the binded circuit.

thetas = ParameterVector('T', length=1)
it = iter(theta)

# Define Hadamard Test
qc = QuantumCircuit(3)
qc.initialize(psi_0, [1,2])


# Apply control gate

tr_g_k = tr_g(dt=next(it))  # tr_g is the evolution gate $\exp(-i\theta H)$
tr_g_c = tr_g_k.control(1)

qc.append(tr_g_c, [0,1,2])

# --------------------------------------------------------
# Get the StateVector

qc_b = qc.bind_parameters({theta: [np.pi]})

simulator = Aer.get_backend('aer_simulator')

qc_bt = transpile(qc_b, simulator)


result = simulator.run(qc_bt).result()
statevector = result.get_statevector(qc_bt)

When running the circuit I get:

TypeError: ParameterExpression with unbound parameters ({ParameterVectorElement(T[0])}) cannot be cast to a float.

I have tried without the control, i.e, only the evolution in the qubit 1 and 2 and it works. I think it's because the control, but so far, I have not been able to solve it.

Thanks for any help !

  • $\begingroup$ Hi Andres, this looks like a bug in Qiskit and I therefore encourage you to post an issue on GitHub: github.com/Qiskit/qiskit-terra. As a temporary workaround, you can try to decompose the circuit and see if that helps: qc_b = qc.decompose().bind_parameters({theta: np.pi}). $\endgroup$
    – Cryoris
    Dec 14 '21 at 13:34
  • $\begingroup$ Thanks for the reply. I replace the line with: qc_b = qc.decompose().bind_parameters({thetas: [np.pi]}) ( [np.pi]) because otherwise I get the error 'TypeError: object of type 'float' has no len()' ) And I still get the error: CircuitError: 'Cannot bind parameters (T[0]) not present in the circuit.' I have even try with the .to_matrix() method of CircuitStateFn of Opflow, and I get the same error. I'd submitted the bug in GitHub. Thanks again. $\endgroup$
    – Andres
    Dec 14 '21 at 14:19

Your Answer

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

Browse other questions tagged or ask your own question.