# Create a mode-entangled state

**URL:** <https://discuss.pennylane.ai/t/create-a-mode-entangled-state/2164>\
**Category:** PennyLane Strawberry Fields\
**Created:** [September 18, 2022, 12:31pm UTC](https://discuss.pennylane.ai/t/create-a-mode-entangled-state/2164 "2022-09-18T12:31:08Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![segev](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/segev/32/527_2.png) [@segev](https://discuss.pennylane.ai/u/segev)\
**Post date:** [September 18, 2022, 12:31pm UTC](https://discuss.pennylane.ai/t/create-a-mode-entangled-state/2164/1 "2022-09-18T12:31:08Z")

</div>

I’m interested in making a passive linear interferometer circuitry acting on an initial mode-entangled state, for example: \frac{1}{\sqrt{2}}(a\_0^{\dagger}a\_2^{\dagger}+a\_1^{\dagger}a\_3^{\dagger}) in a 4 mode systems [0,4].  
What is the best way to create such an initial state? Is there a way to insert it to a circuit without the actual procedure of making it? just define this state as the state of 2 photons in 4 modes and later apply some U on them.

---

<div class="post-metadata">

**Author:** ![CatalinaAlbornoz](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/catalinaalbornoz/32/1196_2.png) [@CatalinaAlbornoz](https://discuss.pennylane.ai/u/CatalinaAlbornoz)\
**Post date:** [September 19, 2022, 7:15pm UTC](https://discuss.pennylane.ai/t/create-a-mode-entangled-state/2164/2 "2022-09-19T19:15:24Z")

</div>

Hi @segev!

We have the [ops.Ket](https://strawberryfields.readthedocs.io/en/stable/code/api/strawberryfields.ops.Ket.html) function which allows you to instantiate the Ket for the state. It prepares the state given the Ket vectors in the Fock basis.

Is this what you were looking for?

---

<div class="post-metadata">

**Author:** ![segev](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/segev/32/527_2.png) [@segev](https://discuss.pennylane.ai/u/segev)\
**Post date:** [September 19, 2022, 8:12pm UTC](https://discuss.pennylane.ai/t/create-a-mode-entangled-state/2164/3 "2022-09-19T20:12:34Z")

</div>

Yes! Thank you @CatalinaAlbornoz!  
Do you have any example of using this in code? I can’t figure out what form of state it expects to get (what are the dimensions of the array and it’s format)

---

<div class="post-metadata">

**Author:** ![CatalinaAlbornoz](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/catalinaalbornoz/32/1196_2.png) [@CatalinaAlbornoz](https://discuss.pennylane.ai/u/CatalinaAlbornoz)\
**Post date:** [September 20, 2022, 9:31pm UTC](https://discuss.pennylane.ai/t/create-a-mode-entangled-state/2164/4 "2022-09-20T21:31:47Z")

</div>

Hi @segev, you can input an array (with the first dimension same as the cutoff dimension) or a [BaseFockState](https://strawberryfields.readthedocs.io/en/stable/code/api/strawberryfields.backends.BaseFockState.html), where you can specify more information.

For example you can choose one of the following examples (note that the input state is not the same):

```
import strawberryfields as sf
import numpy as np

cutoff = 4
eng = sf.Engine("fock", backend_options={"cutoff_dim": cutoff})
prog = sf.Program(4)

with prog.context as q:
    sf.ops.Ket(np.array([1,0,0,1])) | q[0]
                
result = eng.run(prog)
state = result.state
probs = state.all_fock_probs()
print(probs)

```

Or you can do

```
cutoff = 4
eng = sf.Engine("fock", backend_options={"cutoff_dim": cutoff})
state = sf.backends.BaseFockState(np.array([1,0,0,0]), 4, True, cutoff)
prog = sf.Program(4)

with prog.context as q:
    sf.ops.Ket(state) | q[0]
                
result = eng.run(prog)
state = result.state
probs = state.all_fock_probs()
print(probs)

```

If you input an array into sf.ops.Ket, it should be a state vector in the Fock basis. This can be provided as either:

- a single ket vector, for single mode state preparation
- a multimode ket, with one array dimension per mode

If you choose a `BaseFockState` state object as the parameter type, then it already has all of the necessary information.

Does this solve your question?

---

<div class="post-metadata">

**Author:** ![segev](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/segev/32/527_2.png) [@segev](https://discuss.pennylane.ai/u/segev)\
**Post date:** [September 21, 2022, 12:40pm UTC](https://discuss.pennylane.ai/t/create-a-mode-entangled-state/2164/5 "2022-09-21T12:40:55Z")

</div>

Thanks @CatalinaAlbornoz it really helped. However, I’m still struggling with the multimode ket. I’ve tried this format of state:

import strawberryfields as sf  
import numpy as np

cutoff = 2  
eng = sf.Engine(“fock”, backend\_options={“cutoff\_dim”: cutoff})  
prog = sf.Program(4)  
state = sf.backends.BaseFockState(np.array([[0,1],[1,0],[1,0],[0,1]]), 4, True, cutoff)  
with prog.context as q:  
sf.ops.Ket(state) | (q[0],q[1],q[2],q[3])

result = eng.run(prog)  
state = result.state  
probs = state.all\_fock\_probs()  
print(probs)

But it errored with:  
ValueError: cannot reshape array of size 8 into shape (4,4)

Isnt this the multimode ket needed for describing the fock state: |1001\>?

---

<div class="post-metadata">

**Author:** ![CatalinaAlbornoz](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/catalinaalbornoz/32/1196_2.png) [@CatalinaAlbornoz](https://discuss.pennylane.ai/u/CatalinaAlbornoz)\
**Post date:** [September 22, 2022, 11:02pm UTC](https://discuss.pennylane.ai/t/create-a-mode-entangled-state/2164/6 "2022-09-22T23:02:30Z")

</div>

Hi @segev!

In this case, since you have a pure state, then the state representation in the Fock basis for the state |1001〉is just np.array([1,0,0,1]).

So you would just write  
`state = sf.backends.BaseFockState(np.array([1,0,0,1]), 4, True, cutoff)`

Nice and simple! 😄

---

<div class="post-metadata">

**Author:** ![segev](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/segev/32/527_2.png) [@segev](https://discuss.pennylane.ai/u/segev)\
**Post date:** [September 27, 2022, 8:11pm UTC](https://discuss.pennylane.ai/t/create-a-mode-entangled-state/2164/7 "2022-09-27T20:11:32Z")

</div>

Hi @CatalinaAlbornoz, I’ve tried your example and it does seems to work except for the part when I’m trying to print the probabilities of the outcomes:

import strawberryfields as sf  
import numpy as np

cutoff = 2  
eng = sf.Engine(“fock”, backend\_options={“cutoff\_dim”: cutoff})  
prog = sf.Program(4)  
state = sf.backends.BaseFockState(np.array([1,0,0,1]), 4, True, cutoff)  
print(state)  
print(state.data)  
with prog.context as q:  
sf.ops.Ket(state) | (q[0],q[1], q[2], q[3])

result = eng.run(prog)  
state = result.state  
probs = state.all\_fock\_probs()  
print(probs)

And get the error:

ValueError: cannot reshape array of size 4 into shape (4,4)

Which seems very weird since Im not doing any action on any mode except making them in the state

---

<div class="post-metadata">

**Author:** ![CatalinaAlbornoz](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/catalinaalbornoz/32/1196_2.png) [@CatalinaAlbornoz](https://discuss.pennylane.ai/u/CatalinaAlbornoz)\
**Post date:** [September 28, 2022, 6:54pm UTC](https://discuss.pennylane.ai/t/create-a-mode-entangled-state/2164/8 "2022-09-28T18:54:55Z")

</div>

Hi @segev,

I’m not sure why this isn’t working with BaseFockState. But if you change the initial state to the following then it works.

```
modes = 4
state = np.zeros([cutoff] * modes, dtype=complex)
state[1, 0, 0, 1] = 1

```

It’s probably best to keep assigning the state as an array. Let me know if you run into other problems.
