1
$\begingroup$

Trying to create gate, that its angle is parametrized, where my parameter is k, and I want to assign to theta = 1/2**k - 2 in the power of k. Looks like:

k = Parameter('k')
# Build a sub-circuit
q = QuantumRegister(2)
CROT_circ = QuantumCircuit(q, name='CROT')
theta = 2*pi/(2**k) 
CROT_circ.cp(theta, 0, 1)

And I get this error:

TypeError: unsupported operand type(s) for ** or pow(): 'int' and 'Parameter'

Suggestions? Thanks!

$\endgroup$
3
  • 4
    $\begingroup$ I’m voting to close this question because it is not about quantum computing. The question is about operator overloading in python and belongs on StackOverflow. $\endgroup$ Dec 21 '21 at 18:07
  • 2
    $\begingroup$ Actually it is about qiskit specifically, because this is a problem of the Parameter object, which is part of qiskit $\endgroup$
    – Ron Cohen
    Dec 22 '21 at 9:58
  • $\begingroup$ @RonCohen just to clarify regarding the on-topicness: this question is indeed clearly about qiskit, but it is specifically about purely programming aspects of it, as also evident from the answer, which is just about how to write a mathematical expression with numpy. Questions about qiskit are on-topic here, but only as long as they require some knowledge of quantum computation/mechanics to be answered. Purely programming questions can already be asked e.g. on stackoverflow. $\endgroup$
    – glS
    Dec 27 '21 at 7:58
1
$\begingroup$

Using the fact that $2^{-k}=e^{\ln(0.5)k}$,

We can rewrite

theta = 2*pi/(2**k)

to become

theta = 2 * pi * numpy.exp(numpy.log(0.5) * k)
$\endgroup$
1
  • $\begingroup$ Thank you! it helped! $\endgroup$
    – Ron Cohen
    Dec 22 '21 at 9:57

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