# Working with Subspace

Due to the strong Rydberg interactions, only one Rydberg excitation is allowed if the atoms are close to each other. We typically take this as the blockade radius, $R_b$, which is the distance for which the Rydberg interaction is the same as the Rabi frequency, $\Omega$ (see Rydberg Blockade). This is the so-called blockade constraint.

In Bloqade, we can take advantage of this effect by allowing users to run emulation in a truncated subspace, i.e., by throwing out states that violate the blockade constraint. This can help accelerate the simulation and enables simulation for a larger system size. In this section, we will show how to create a blockade subspace, create registers in the subspace, obtain the Hamiltonian matrix in the subspace, and run emulation in the subspace.

Note that the blockade radius $R_b$ is the distance for which the Rydberg interaction is the same as the Rabi frequency, $\Omega$. For accurate simulation, however, it's not recommended to throw away the states that's close to the blockade radius. In other words, it's safer to set the subspace radius $R_s$ to be smaller than $R_b$, where we throw away the blockade violated states when the atoms are within $R_s$. For example, if we set $R_s = 1/2 * R_b$, we will be throwing away states that have interaction energies at least $2^6*\Omega$, which will be a good approximation. See the Rydberg Blockade page for recommendations on how to set $R_b$, $R_s$, and the atom lattice separation, $a$.

## Create the Blockade Subspace

One can create a blockade subspace via the `blockade_subspace`

method if we know the atomic positions:

`BloqadeMIS.blockade_subspace`

— Function`blockade_subspace(atoms[, radius=1.0])`

Create a blockade approximation subspace from given atom positions and radius.

For example, we can construct a blockade subspace of a square lattice using the code below:

```
using Bloqade
atoms = generate_sites(SquareLattice(), 3, 3, scale=5.1)
space = blockade_subspace(atoms, 5.2)
```

```
9-qubits 63-elements Subspace{Int64, Vector{Int64}}:
───┬────
1│ 0
2│ 1
3│ 2
⋮│ ⋮
61│ 337
62│ 340
63│ 341
```

We first created a $3*3$ square lattice with nearest neighbor atoms separated by $5.1$ μm. Then we have created a blockade subspace with the subspace radius, $R_s$, being $5.2$ μm. This means that if two atoms have a separation distance that is smaller than (or equal to) $5.2$ μm, then the blockade subspace does not contain states where both of them being in the Rydberg states. For the dictionary shown, the left is the new index of the states in the blockade subspace; in this case, there are 63 allowed states, which is much smaller than the full Hilbert space size 512. The vectors on the right correspond to the base-10 representations of the states in bitstrings.

Here `space`

is of the type `Subspace`

:

`BloqadeExpr.Subspace`

— Type`Subspace{S <: AbstractVector} <: AbstractSpace`

A `Dict`

-like object stores the mapping between subspace and full space.

Other than using atomic positions and the subspace radius, we can also use a graph to create a subspace. In this case, the subspace corresponds to the space composed by the independent sets of this graph. Bloqade has an explicit function for this, by using a graph as an input, and produces the subspace as the output. Here is an example code:

```
using Graphs
g = SimpleGraph(5)
edge_set = [(1,2), (1, 4), (2, 5), (3, 4)]
for (i,j) in edge_set
add_edge!(g, i, j)
end
space = independent_set_subspace(g)
```

```
5-qubits 13-elements Subspace{Int64, Vector{Int64}}:
───┬───
1│ 0
2│ 1
3│ 2
⋮│ ⋮
11│ 20
12│ 21
13│ 24
```

## Create Registers in the Subspace

One can create a register in the subspace by feeding the `space`

object instead of an integer for the common register interfaces, e.g.:

`julia> zero_state(space)`

`SubspaceArrayReg{2, ComplexF64, Vector{ComplexF64}, Subspace{Int64, Vector{Int64}}}(5, ComplexF64[1.0 + 0.0im, 0.0 + 0.0im, 0.0 + 0.0im, 0.0 + 0.0im, 0.0 + 0.0im, 0.0 + 0.0im, 0.0 + 0.0im, 0.0 + 0.0im, 0.0 + 0.0im, 0.0 + 0.0im, 0.0 + 0.0im, 0.0 + 0.0im, 0.0 + 0.0im], Subspace{Int64, Vector{Int64}}(5, Dict(5 => 5, 16 => 9, 20 => 11, 24 => 13, 8 => 7, 17 => 10, 1 => 2, 0 => 1, 6 => 6, 4 => 4…), [0, 1, 2, 4, 5, 6, 8, 10, 16, 17, 20, 21, 24]))`

`julia> product_state(bit"000_000_001", space)`

`SubspaceArrayReg{2, ComplexF64, Vector{ComplexF64}, Subspace{Int64, Vector{Int64}}}(5, ComplexF64[0.0 + 0.0im, 1.0 + 0.0im, 0.0 + 0.0im, 0.0 + 0.0im, 0.0 + 0.0im, 0.0 + 0.0im, 0.0 + 0.0im, 0.0 + 0.0im, 0.0 + 0.0im, 0.0 + 0.0im, 0.0 + 0.0im, 0.0 + 0.0im, 0.0 + 0.0im], Subspace{Int64, Vector{Int64}}(5, Dict(5 => 5, 16 => 9, 20 => 11, 24 => 13, 8 => 7, 17 => 10, 1 => 2, 0 => 1, 6 => 6, 4 => 4…), [0, 1, 2, 4, 5, 6, 8, 10, 16, 17, 20, 21, 24]))`

Alternatively, if you have an existing state stored as a subtype of `AbstractVector`

, you can also create the register using the constructor:

`julia> state = rand(ComplexF64, length(space))`

`13-element Vector{ComplexF64}: 0.4795003342792641 + 0.6631896518246128im 0.8190597109660283 + 0.9992233532899898im 0.1419190072356381 + 0.09163854282348549im 0.1025599395234722 + 0.49809097511584866im 0.6612687922814764 + 0.45210126443605214im 0.04732842575497409 + 0.19237099382774792im 0.7703800125239054 + 0.3914174274346641im 0.20435024031704263 + 0.2484848157317816im 0.28613365933343293 + 0.43511195341291564im 0.4525375075606569 + 0.3390328350261458im 0.6250407030005422 + 0.5261036092098529im 0.7652266766241209 + 0.12406319387151721im 0.345766295321552 + 0.6035979028557129im`

`julia> reg = SubspaceArrayReg(state, space)`

`SubspaceArrayReg{2, ComplexF64, Vector{ComplexF64}, Subspace{Int64, Vector{Int64}}}(5, ComplexF64[0.4795003342792641 + 0.6631896518246128im, 0.8190597109660283 + 0.9992233532899898im, 0.1419190072356381 + 0.09163854282348549im, 0.1025599395234722 + 0.49809097511584866im, 0.6612687922814764 + 0.45210126443605214im, 0.04732842575497409 + 0.19237099382774792im, 0.7703800125239054 + 0.3914174274346641im, 0.20435024031704263 + 0.2484848157317816im, 0.28613365933343293 + 0.43511195341291564im, 0.4525375075606569 + 0.3390328350261458im, 0.6250407030005422 + 0.5261036092098529im, 0.7652266766241209 + 0.12406319387151721im, 0.345766295321552 + 0.6035979028557129im], Subspace{Int64, Vector{Int64}}(5, Dict(5 => 5, 16 => 9, 20 => 11, 24 => 13, 8 => 7, 17 => 10, 1 => 2, 0 => 1, 6 => 6, 4 => 4…), [0, 1, 2, 4, 5, 6, 8, 10, 16, 17, 20, 21, 24]))`

## Obtain the Hamiltonian Matrix in the Subspace

The matrix projected in the subspace of a given Hamiltonian can be obtained via `mat`

as well, e.g.:

`julia> atoms = generate_sites(SquareLattice(), 3, 3, scale=5.1);`

`julia> space = blockade_subspace(atoms, 5.2);`

`julia> h1 = rydberg_h(atoms; Δ=2.0*2π, Ω=1.0*2π)`

`nqubits: 9 + ├─ [+] ∑ 2π ⋅ 8.627e5.0/|x_i-x_j|^6 n_i n_j ├─ [+] 2π ⋅ 0.5 ⋅ ∑ σ^x_i └─ [-] 2π ⋅ 2.0 ⋅ ∑ n_i`

`julia> mat(h1, space)`

`63×63 SparseMatrixCSC{ComplexF64, Int64} with 366 stored entries: ⎡⡞⢍⡊⢄⠑⡀⠑⠄⠈⠢⡀⠀⠀⠀⠈⠢⡀⠀⠀⠀⠀⠑⢄⠀⠀⠀⠀⠀⠀⠀⠀⠀⎤ ⎢⠊⢌⢱⢖⠀⠈⠀⠐⠄⠀⠈⠀⠀⠀⠀⠀⠈⠢⡀⠀⠀⠀⠀⠑⢄⠀⠀⠀⠀⠀⠀⠀⎥ ⎢⠑⠠⡀⠀⢟⣵⠀⠀⠀⠀⠀⠑⢄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠑⢄⠀⠀⠀⠀⠀⎥ ⎢⠑⠄⢀⠀⠀⠀⡟⢍⡄⠀⠀⠀⠀⠑⠄⠀⠀⠀⠈⠢⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⎥ ⎢⠢⡀⠀⠁⠀⠀⠀⠉⢱⢖⠢⠢⠀⠢⡀⠀⠀⠀⠀⠀⠈⠀⠀⠀⠀⠀⠀⠢⡀⠀⠀⠀⎥ ⎢⠀⠈⠂⠀⢄⠀⠀⠀⠨⡂⠛⣤⢕⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠢⡀⠀⎥ ⎢⠀⠀⠀⠀⠀⠑⢄⠀⠠⡀⠑⠑⠛⣤⠄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠂⎥ ⎢⠢⡀⠀⠀⠀⠀⠀⠁⠀⠈⠀⠀⠀⠁⢱⢖⠢⢂⠐⢄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⎥ ⎢⠀⠈⠢⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠨⢂⠛⣤⠅⠀⢄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⎥ ⎢⠀⠀⠀⠈⠀⠀⠢⡀⠀⠀⠀⠀⠀⠀⠐⢄⠁⠁⢱⢖⢂⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⎥ ⎢⢄⠀⠀⠀⠀⠀⠀⠈⠂⠀⠀⠀⠀⠀⠀⠀⠀⠑⠈⠐⠛⣤⠤⡠⠀⢄⠀⢄⠀⠀⠀⠀⎥ ⎢⠀⠑⢄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡣⠱⢆⣑⠀⠢⠀⠑⠄⠀⠀⎥ ⎢⠀⠀⠀⠑⢄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢄⠑⠘⠑⣤⢄⠀⠀⠠⡀⠀⎥ ⎢⠀⠀⠀⠀⠀⠑⠀⠀⠠⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢄⠈⠂⠀⠑⠛⣤⠤⡠⡈⠂⎥ ⎢⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠢⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠑⠄⠀⡀⠀⡣⠱⢆⣐⠄⎥ ⎣⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠢⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠢⠈⠐⠜⠵⠇⎦`

## Other Operations in the Subspace

All other operations in the subspace are the same as the fullspace case. For example, to run an emulation in the subspace, one just need to use the subspace register `SubspaceArrayReg`

instead of the fullspace register `ArrayReg`

. The rest of the code are the same:

```
reg = zero_state(space)
prob = SchrodingerProblem(reg, 1.0, h1)
emulate!(prob)
statevec(reg)
```

```
63-element Vector{ComplexF64}:
0.1358530785038882 + 0.7346213876906682im
-0.002545006710187195 + 0.03271936107717212im
-0.09136564439212715 + 0.14709932277558385im
-0.002545006710187521 + 0.03271936107717173im
0.03640229629720827 - 0.04785883792433143im
-0.09136564439212733 + 0.14709932277558377im
-0.0035203378092491036 - 0.1335118776601369im
-0.009182822658231478 + 0.03774190675616856im
0.21572816867719474 - 0.10378379639436636im
-0.0010523745450195178 - 0.10411131375242307im
⋮
0.03640229629720847 - 0.04785883792433164im
0.07737130739563985 - 0.03276442008301344im
-0.0071961924407432385 - 0.02287290669803617im
0.07737130739564019 - 0.032764420083013356im
0.13361821949331412 - 0.0565760645311052im
-0.002309134772695679 + 0.021944696595877264im
-0.0018899409335649169 - 0.001978434757357867im
-0.0018899409335649887 - 0.0019784347573579032im
-0.0029665958292894264 + 0.0016052003624637075im
```

Measurements on the subspace register is the same as that in the full space.

## Create Constrained Local Hamiltonians in the Subspace

Although we are able to emulate our Hamiltonian problem in the projected subspace, the long-range tail of the Rydberg interactions will be present in the subspace Hamiltonian. In certain cases, you may not want the long-range tail by only simulating a constrained short-range Hamiltonian, e.g. the PXP model. In this case, we can use Bloqade to easily deal with such problems for an arbitrary graph in an arbitrary dimension.

Let us take the PXP model in 1D as an example. We first create a 1D chain and then generate a subspace by projecting out states that have nearest-neighbor interactions.

```
atoms = generate_sites(ChainLattice(), 10, scale=5.1)
space = blockade_subspace(atoms, 5.2)
register = product_state(bit"0101010101", space)
h = 2π * 4.0 * SumOfX(length(atoms)) - 2π * 1.0 * SumOfN(length(atoms))
prob = SchrodingerProblem(register, 0.2, h)
emulate!(prob)
```

```
SchrodingerProblem:
register info:
type: SubspaceArrayReg{2, ComplexF64, Vector{ComplexF64}, Subspace{Int64, Vector{Int64}}}
storage size: 40 bytes
time span (μs): (0.0, 0.2)
equation:
storage size: 16.492 KiB
expression:
nqubits: 10
+
├─ [scale: 25.132741228718345] ∑ σ^x_i
└─ [scale: -6.283185307179586] ∑ n_i
algorithm: DP8(; stage_limiter! = trivial_limiter!, step_limiter! = trivial_limiter!, thread = static(false),)
options:
save_everystep: false
save_start: false
save_on: false
dense: false
reltol: 1.0e-10
abstol: 1.0e-10
```

After creating the subspace, we have built a Hamiltonian by explicitly summing up the Rabi frequency term and the detuning term by using `SumOfX`

and `SumOfN`

respectively. In this way, we have created a local constraint Hamiltonian (without the long-range interaction tail). Futhermore, if we want to emulate quantum dynamics under this Hamiltonian, we just need to create a subspace register and emulate the system under the created Hamiltonian.