# Initialize state for a quantum circuit

**URL:** <https://itensor.discourse.group/t/initialize-state-for-a-quantum-circuit/2282>\
**Category:** ITensor Julia Questions\
**Tags:** julia, entanglement, mps\
**Created:** [March 5, 2025, 6:36am UTC](https://itensor.discourse.group/t/initialize-state-for-a-quantum-circuit/2282 "2025-03-05T06:36:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shachar](https://avatars.discourse-cdn.com/v4/letter/s/bbce88/32.png) [@Shachar](https://itensor.discourse.group/u/Shachar)\
**Post date:** [March 5, 2025, 6:36am UTC](https://itensor.discourse.group/t/initialize-state-for-a-quantum-circuit/2282/1 "2025-03-05T06:36:58Z")

</div>

Hi,

I’m new to ITensors and the entire field of MPS and tensor networks, so I might be missing something fundamental.

I’m trying to write code that simulates quantum gates, but when I attempt to initialize an entangled state, the results are not as expected. I referred to this discussion: MPS Initialization Through State Vector and wrote the following code:

```auto
N = 2
A = normalize([1, 1, 0, 0])
sites = siteinds("Qubit",N)

m = MPS(A, sites)
gates_mpo = ops(sites, [("H", 1)])
ψ = apply(gates_mpo, m)

```

After applying the gate, I used the `sample` function multiple times (or a custom function based on the example here [MPS and MPO Examples · ITensors.jl](https://itensor.github.io/ITensors.jl/dev/examples/MPSandMPO.html) that returns the amplitude vector). However, the results were always limited to the state `[1,1]`, or the amplitude vector:

```auto
4-element Vector{ComplexF64}:
 0.9999999999999998 + 0.0im
                0.0 + 0.0im
                0.0 + 0.0im
                0.0 + 0.0im

```

This does not match the expected outcome. It seems as though the Hadamard gate was applied to the second qubit instead of the first.

However, when I replaced A with a computational basis vector, such as `[1, 0, 0, 0]`, the result was as expected:

```auto
4-element Vector{ComplexF64}:
 0.7071067811865475 + 0.0im
                0.0 + 0.0im
 0.7071067811865475 + 0.0im
                0.0 + 0.0im

```

I tried constructing the MPS as a sum of computational basis MPS states, weighted by their corresponding amplitudes. This approach worked, but I’m concerned that it might not be efficient in terms of runtime.

I would appreciate any insights into why this happens. Thanks!

---

<div class="post-metadata">

**Author:** ![VinceNeede](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/vinceneede/32/731_2.png) [@VinceNeede](https://itensor.discourse.group/u/VinceNeede)\
**Post date:** [March 5, 2025, 8:15am UTC](https://itensor.discourse.group/t/initialize-state-for-a-quantum-circuit/2282/2 "2025-03-05T08:15:14Z")

</div>

Hi @Shachar,  
I think there is a misunderstanding on the basis used.

> **Long explanation on the basis used**
>
> The basis used is the usual lexicographic computational basis, i.e. in your case, the first element of the vector A is mapped to the state (1,1)
> 
> | Starting index | mapped index |
> | --- | --- |
> | 1 | (1,1) |
> | 2 | (2,1) |
> | 3 | (1,2) |
> | 4 | (2,2) |
> 
> Which means that the vector you are passing is interpreted as the state (in bra-ket) notation
> 
> \frac 1{\sqrt{2}} (\ket{00} + \ket{10})
> 
> Or calling \ket{+}=(\ket0+\ket1), your starting state is:
> 
> \psi\_0=\ket{+}\ket{0}
> 
> Since the Hamard Gate maps \ket{+}=\>\ket0 by applying it to the first site in the state \psi\_0 you obtain the state \ket{00}, which is coherent with your sampling results.
> 
> If you are interested to the state
> 
> \ket{\phi^+}=\frac 1{\sqrt{2}}(\ket{00} +\ket{11})
> 
> Using the table above, this is obtained from the vector `A=normalize([1. 0. 0. 1.])`.

Probably the most user friendly solution is to construct the `ITensor` first as follows:

```auto
s1, s2 = siteinds("Qubit", 2)
A=ITensor(s1, s2)
A[s1=>1, s2=>1] = 1.
A[s1=>2, s2=>2] =1.
psi = normalize(MPS(A, (s1, s2)))

```

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/itensor/original/1X/d3072b13e047cd06df7f3981547c0917d940dfa4.png) [@system](https://itensor.discourse.group/u/system)\
**Post date:** [March 15, 2025, 8:15am UTC](https://itensor.discourse.group/t/initialize-state-for-a-quantum-circuit/2282/3 "2025-03-15T08:15:40Z")

</div>

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
