# Using mixed states as input

**URL:** <https://discuss.pennylane.ai/t/using-mixed-states-as-input/2092>\
**Category:** PennyLane Help\
**Created:** [August 9, 2022, 5:54pm UTC](https://discuss.pennylane.ai/t/using-mixed-states-as-input/2092 "2022-08-09T17:54:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jackaraz](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/jackaraz/32/852_2.png) [@jackaraz](https://discuss.pennylane.ai/u/jackaraz)\
**Post date:** [August 9, 2022, 5:54pm UTC](https://discuss.pennylane.ai/t/using-mixed-states-as-input/2092/1 "2022-08-09T17:54:23Z")

</div>

Hi all, I’ve seen a couple of questions regarding circuits with mixed state initialization, but I have some further questions. I would like to use `QubitDensityMatrix` to implement my mixed state as input however, I realised a couple of problems that I could not solve. It seems like `QubitDensityMatrix` is only available for “default.mixed” devices. I’ve seen [here](http://discuss.pennylane.ai/t/initialize-circuit-with-a-mixed-state/845/2) that mixed states are also available in “cirq.mixedsimulator” device but `qml.device("cirq.mixedsimulator", wires=2).operations` does not have `QubitDensityMatrix`. I want to use it in TensorFlow because, for some reason, I can not take the gradient of my expectation value if I use `scipy.linalg.expm` to construct my hamiltonian. Here is a simplified example;

Imagine that I have a trainable function which creates my Hamiltonian, and I need to take the gradient of the expectation value wrt the parameters of this function and quantum network. Here is some code

```python
from scipy.linalg import expm

dev = qml.device("default.mixed", wires = 2)

rho = np.zeros((2 **2,2** 2), dtype=np.complex128)
rho[0,0] = 1.
state = np.random.uniform(0.1, 1,(2**2, 1))
state /= np.linalg.norm(state)
energy = -3.
phi = np.random.uniform(0, 1,(2,))

@qml.qnode(dev)
def circuit(rho: np.ndarray, phi: np.ndarray):
    qml.QubitDensityMatrix(rho, wires=range(2))
    qml.RY(phi[0], 0)
    qml.RY(phi[1], 1)
    qml.CNOT(wires=[0,1])
    return qml.density_matrix(wires=range(2))

def cost(rho, phi,state, energy):
    Hamiltonian = energy * (state @ np.conj(state).T)
    density = circuit(rho, phi)
    return np.real(np.trace(density @ expm(-Hamiltonian)))
    
grads = qml.grad(cost)
g=grads(rho, phi, state, energy)

```

Here since my Hamiltonian is an ArrayBox, this code crushes. But without ` expm(-Hamiltonian)`, I get a result.

The reason why I didn’t use `qml.expval(qml.Hermitian(Hamiltonian, wires=range(2)))` is because I again got an Arraybox-related issue where this time, somewhere in the code, it’s trying to take conjugate of the matrix which does not exist since its an ArrayBox. Note that this works if I run `cost(rho, phi, state, energy)` without gradient. My only issue is with the gradient. So I thought the simplest workaround would be to externalize the gradients to TensorFlow.

Is there any workaround that I can use? In the worst-case scenario, is there a generic algorithm to build a gate structure that creates a mixed state input?

Thanks

---

<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:** [August 9, 2022, 9:33pm UTC](https://discuss.pennylane.ai/t/using-mixed-states-as-input/2092/2 "2022-08-09T21:33:19Z")

</div>

Hey @jackaraz! Thanks for the question. To your code example, I was able to get your code working by ensuring that `requires_grad` is specified in every argument that you’re passing to your cost function. Specifically:

```python
rho = np.zeros((2 **2,2** 2), dtype=np.complex128, requires_grad=False)
rho[0,0] = 1.
state = np.random.uniform(0.1, 1,(2**2, 1), requires_grad=False)
state /= np.linalg.norm(state)
energy = -3.
phi = np.random.uniform(0, 1,(2,), requires_grad=True)

```

---

<div class="post-metadata">

**Author:** ![jackaraz](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/jackaraz/32/852_2.png) [@jackaraz](https://discuss.pennylane.ai/u/jackaraz)\
**Post date:** [August 10, 2022, 7:13am UTC](https://discuss.pennylane.ai/t/using-mixed-states-as-input/2092/3 "2022-08-10T07:13:35Z")

</div>

Thanks @isaacdevlugt; I also managed to utilize tensorflow. Once the interface of the circuit is set to “tf” it seems to work with the “default.mixed” device.

But on a side note, if you know a reference on how to write a gate structure to embed a random multi-qubit mixed state density matrix on a quantum circuit, I would very much like to learn.

---

<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:** [August 11, 2022, 4:06pm UTC](https://discuss.pennylane.ai/t/using-mixed-states-as-input/2092/4 "2022-08-11T16:06:59Z")

</div>

Having consulted a few people internally at Xanadu, it depends on what you mean by “random”. Here are a few resources:

> **[Emergent quantum state designs from individual many-body wavefunctions](https://arxiv.org/abs/2103.03536)**
>
> Quantum chaos in many-body systems provides a bridge between statistical and
> quantum physics with strong predictive power. This framework is valuable for
> analyzing properties of complex quantum systems such as energy spectra and the
> dynamics of...

> **[Generating and using truly random quantum states in Mathematica](https://arxiv.org/abs/1102.4598)**
>
> The problem of generating random quantum states is of a great interest from
> the quantum information theory point of view. In this paper we present a
> package for Mathematica computing system harnessing a specific piece of
> hardware, namely Quantis...

[https://qutip.org/docs/4.0.2/guide/guide-random.html](https://qutip.org/docs/4.0.2/guide/guide-random.html)

I think the simplest thing you can do is to create a random _pure_ state with twice as many qubits and trace over half of the system (I’m trivializing a little here…).

Let us know if any of this helps!
