# Cannot train model when get the embedding state from qiskit

**URL:** <https://discuss.pennylane.ai/t/cannot-train-model-when-get-the-embedding-state-from-qiskit/4154>\
**Category:** PennyLane Help\
**Created:** [February 26, 2024, 3:48am UTC](https://discuss.pennylane.ai/t/cannot-train-model-when-get-the-embedding-state-from-qiskit/4154 "2024-02-26T03:48:29Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tianaaa](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/tianaaa/32/2341_2.png) [@Tianaaa](https://discuss.pennylane.ai/u/Tianaaa)\
**Post date:** [February 26, 2024, 3:48am UTC](https://discuss.pennylane.ai/t/cannot-train-model-when-get-the-embedding-state-from-qiskit/4154/1 "2024-02-26T03:48:30Z")

</div>

I am training a MNIST classification hybrid model, there are some parts that I would like to obtain a state from qiskit and embed it into Pennylane’s circuit.

```auto
def data_prepration(d):
    for i,j in enumerate(d):
        r = j
        temp = get_state_fromqiskit(r*np.pi)
       # temp = get_state_frompennylane(r*np.pi)

        temp2 = torch.tensor(temp)
    
        qml.AmplitudeEmbedding(temp2, wires=i)

```

Where

```auto
def get_state_pennylane(x):
    dev = qml.device("default.qubit", wires=1)

    @qml.qnode(dev)
    def circuit(x):
        qml.RY(x, wires=0)
        return qml.state()

    statevector = circuit(x)

    return statevector
def get_state_fromqiskit(x):

    qc = QuantumCircuit(1)
    x = float(x)
    qc.ry(x, 0)

    backend = Aer.get_backend('statevector_simulator')
    job = execute(qc, backend)
    result = job.result()

    statevector = result.get_statevector()
    statevector = np.array(statevector).tolist()
    return statevector

```

The code for obtaining the state above is just a simple example,When i use get\_state\_pennylane, it works normally, when i use get\_state\_fromqiskit, it’s not.Could you answer my question？ Thanks!  
qml.about():  
Platform info: macOS-13.3-arm64-arm-64bit  
Python version: 3.10.13  
Numpy version: 1.23.5  
Scipy version: 1.11.3  
Installed devices:

- qiskit.aer (PennyLane-qiskit-0.34.0)
- qiskit.basicaer (PennyLane-qiskit-0.34.0)
- qiskit.ibmq (PennyLane-qiskit-0.34.0)
- qiskit.ibmq.circuit\_runner (PennyLane-qiskit-0.34.0)
- qiskit.ibmq.sampler (PennyLane-qiskit-0.34.0)
- qiskit.remote (PennyLane-qiskit-0.34.0)
- lightning.qubit (PennyLane-Lightning-0.34.0)
- default.gaussian (PennyLane-0.34.0)
- default.mixed (PennyLane-0.34.0)
- default.qubit (PennyLane-0.34.0)
- default.qubit.autograd (PennyLane-0.34.0)
- default.qubit.jax (PennyLane-0.34.0)
- default.qubit.legacy (PennyLane-0.34.0)
- default.qubit.tf (PennyLane-0.34.0)
- default.qubit.torch (PennyLane-0.34.0)
- default.qutrit (PennyLane-0.34.0)
- null.qubit (PennyLane-0.34.0)

---

<div class="post-metadata">

**Author:** ![isaacdevlugt](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/isaacdevlugt/32/1159_2.png) [@isaacdevlugt](https://discuss.pennylane.ai/u/isaacdevlugt)\
**Post date:** [February 26, 2024, 9:48pm UTC](https://discuss.pennylane.ai/t/cannot-train-model-when-get-the-embedding-state-from-qiskit/4154/2 "2024-02-26T21:48:52Z")

</div>

Hey @Tianaaa, welcome to the forum! 🌞

If I understand your question correctly, I think you’re after `qml.from_qiskit`: [qml.from\_qiskit — PennyLane 0.34.0 documentation](https://docs.pennylane.ai/en/stable/code/api/pennylane.from_qiskit.html). It allows you to take qiskit circuits and turn them into usable elements in pennylane 🙂. We also have some more pennylane-qiskit features and improvements coming with the next release (next week)!

Let me know if this helps 🙂

---

<div class="post-metadata">

**Author:** ![Tianaaa](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/tianaaa/32/2341_2.png) [@Tianaaa](https://discuss.pennylane.ai/u/Tianaaa)\
**Post date:** [February 27, 2024, 10:56am UTC](https://discuss.pennylane.ai/t/cannot-train-model-when-get-the-embedding-state-from-qiskit/4154/3 "2024-02-27T10:56:16Z")

</div>

Hi,@isaacdevlugt,thanks for your reply! 😄  
I have tried `qml.from_qiskit`, Here is my code:

```auto
def get_state_fromqiskit(x):

    qc = QuantumCircuit(1)
    x = float(x)
    qc.ry(x, [0])
    my_circuit = qml.from_qiskit(qc)
    dev = qml.device('default.qubit', wires=1)
    @qml.qnode(dev)
    def circuit():
        my_circuit(wires=[0])
        return qml.state()
    return circuit()

```

But the model still can’t be trained, the demo I modified is based on [hybrid-quantum-classifier-in-pennylane-for-MNIST-dataset-based-on-inverse-MERA/pytorch\_pennylane\_hybrid\_binary\_classifier.ipynb at main · amirm343/hybrid-quantum-classifier-in-pennylane-for-MNIST-dataset-based-on-inverse-MERA · GitHub](https://github.com/amirm343/hybrid-quantum-classifier-in-pennylane-for-MNIST-dataset-based-on-inverse-MERA/blob/main/pytorch_pennylane_hybrid_binary_classifier.ipynb). I really have no idea where the problem is.

By the way, the use of Ry in the code I presented above is just a simple example. My actual requirement is to measure several entangled bits , obtain the collapsed state of the remaining one and embed it in the circuit.

---

<div class="post-metadata">

**Author:** ![isaacdevlugt](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/isaacdevlugt/32/1159_2.png) [@isaacdevlugt](https://discuss.pennylane.ai/u/isaacdevlugt)\
**Post date:** [February 27, 2024, 11:17pm UTC](https://discuss.pennylane.ai/t/cannot-train-model-when-get-the-embedding-state-from-qiskit/4154/4 "2024-02-27T23:17:59Z")

</div>

Thanks! Let’s try to stick with the simple example that you wrote here and work our way towards your application 🙂

Are you trying to modify `x`? Like that’s the variable you want to train and that’s it?

---

<div class="post-metadata">

**Author:** ![Tianaaa](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/tianaaa/32/2341_2.png) [@Tianaaa](https://discuss.pennylane.ai/u/Tianaaa)\
**Post date:** [February 28, 2024, 3:19am UTC](https://discuss.pennylane.ai/t/cannot-train-model-when-get-the-embedding-state-from-qiskit/4154/5 "2024-02-28T03:19:30Z")

</div>

Thanks for your reply! 😃  
The complete qnode(qlayer) in the [initial demo](https://github.com/amirm343/hybrid-quantum-classifier-in-pennylane-for-MNIST-dataset-based-on-inverse-MERA/blob/main/pytorch_pennylane_hybrid_binary_classifier.ipynb) concatenated two parts:

```auto
@qml.qnode(dev)
def inverce_MERA_layer(inputs, mera_parameters):

    data_prepration(inputs)

    inverce_MERA(mera_parameters)

    return qml.expval(qml.PauliZ(0))

```

`inverce_MERA` is a vqc which contains trainable variables, and `data_prepration` is just a data-embeddding circuit(the initial demo used Angle Embedding):

```auto
#state prepration template
def data_prepration(d):

    for i,j in enumerate(d):
        r = j/510
        qml.RY(r*np.pi, wires=i)
    
    qml.Barrier(wires=(len(d)-1,0))

```

I just want to modify this ,not directly using `qml.RY` to embed data , but using qiskit or pennylane-qiskit to obtain the embedded state of Ry() data and setting it as the initial state of the circuit with `qml.AmplitudeEmbedding` or `qml.QubitStateVector` .  
But with all my efforts, I couldn’t do it successfully. No matter what I do, as long as I don’t use Pennylane(`get_state_pennylane`in my first post) to obtain this state, the loss won’t decrease 😥

---

<div class="post-metadata">

**Author:** ![isaacdevlugt](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/isaacdevlugt/32/1159_2.png) [@isaacdevlugt](https://discuss.pennylane.ai/u/isaacdevlugt)\
**Post date:** [February 28, 2024, 11:07pm UTC](https://discuss.pennylane.ai/t/cannot-train-model-when-get-the-embedding-state-from-qiskit/4154/6 "2024-02-28T23:07:19Z")

</div>

Hey! I’m confused 😅. I think I just need to see your code, try and run it, replicate the behaviour, etc. If you could attach something minimal here that still reproduces what you’re seeing, that would be lovely 🙏! Sorry this is taking a while 😓

---

<div class="post-metadata">

**Author:** ![Tianaaa](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/tianaaa/32/2341_2.png) [@Tianaaa](https://discuss.pennylane.ai/u/Tianaaa)\
**Post date:** [March 7, 2024, 10:38am UTC](https://discuss.pennylane.ai/t/cannot-train-model-when-get-the-embedding-state-from-qiskit/4154/7 "2024-03-07T10:38:20Z")

</div>

Sorry for replying so late, sorry again that i couldn’t give the minimal, but you can see [this](https://github.com/amirm343/hybrid-quantum-classifier-in-pennylane-for-MNIST-dataset-based-on-inverse-MERA/blob/main/pytorch_pennylane_hybrid_binary_classifier.ipynb).  
What I want to do is just not use pennylane Ry embedding directly, but obtain the state of equivalent Ry embedding using qiskit then use Pennylane` AmplitudeEmbedding` to take it as the initial state.  
I think these two methods are the same.But the latter’s loss does not decrease.

---

<div class="post-metadata">

**Author:** ![isaacdevlugt](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/isaacdevlugt/32/1159_2.png) [@isaacdevlugt](https://discuss.pennylane.ai/u/isaacdevlugt)\
**Post date:** [March 8, 2024, 6:57pm UTC](https://discuss.pennylane.ai/t/cannot-train-model-when-get-the-embedding-state-from-qiskit/4154/8 "2024-03-08T18:57:11Z")

</div>

Hey @Tianaaa,

It’s still not clear to me what you’re trying to accomplish 😅. Maybe some of our new features will help?

> **[Importing workflows](https://docs.pennylane.ai/en/stable/introduction/importing_workflows.html#qiskit)**
>
> PennyLane supports importing quantum circuits and operations that were constructed using another framework. This includes circuits defined using Qiskit, OpenQASM, and Quil. Importing quantum circui...

It sounds like you’re trying to take a state from Qiskit, embed it into a PennyLane circuit as the initial state, and then train _something_.

This function should work for the first part (qiskit state → PennyLane initial state):

```auto
def get_state_fromqiskit(x):

    qc = QuantumCircuit(1)
    x = float(x)
    qc.ry(x, [0])
    my_circuit = qml.from_qiskit(qc)
    dev = qml.device('default.qubit', wires=1)
    @qml.qnode(dev)
    def circuit():
        my_circuit(wires=[0])
        return qml.state()
    return circuit()

```

> But the latter’s loss does not decrease.

What are you trying to train? Can we work with a toy example?
