2
$\begingroup$

In Pennylane, I created a function to set the initial parameters of my qubit, and then called qml.state() to return the output. I now want to feed this initialization into another circuit, but I'm not sure how I would go about doing that.

here's what I have:

random_init() initializes a set of qubits to particular states, and then returns qml.state()

@qml.qnode(dev1)
def random_init():
    for i  in range(0, y, 3):
            qml.RX(random_list[i], i//3)
            qml.RY(random_list[i+1], i//3)
            qml.RZ(random_list[i+2], i//3)
    return qml.state()

I now want to take qml.state() and pass it through another circuit that does computations based on the initial states set by random_init().

Is this possible to do? Furthermore, is there a more effective way to achieve this goal?

Thanks in advance for any help people provide.

$\endgroup$

1 Answer 1

2
$\begingroup$

You can totally do this in PennyLane. Check out qml.QubitStateVector :)


import pennylane as qml
from pennylane import numpy as np

dev1 = qml.device("default.qubit", wires=1)

@qml.qnode(dev1)
def circuit():
    qml.RX(np.pi/3, wires=0)
    return qml.state()

@qml.qnode(dev1)
def new_circuit(state):
    qml.QubitStateVector(state, wires=0)
    return qml.state()


state = circuit()
print(new_circuit(state))

# Out: [0.8660254+0.j  0.       -0.5j]

Hope this helps!

$\endgroup$
3
  • $\begingroup$ Elegant! Thanks so much. $\endgroup$
    – TuktukTaxi
    Nov 1, 2022 at 0:41
  • $\begingroup$ Would QubitStateVector work for entangled states as well? $\endgroup$
    – TuktukTaxi
    Nov 1, 2022 at 19:25
  • $\begingroup$ Yes! All QubitStateVector needs is a vector in the computational basis. It could be entangled, separable (not entangled), etc. For density matrices, see QubitDensityMatrix. $\endgroup$
    – isaac
    Nov 2, 2022 at 14:24

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.