using Yao
using YaoBlocks.Optimise: replace_block
using CairoMakie

Noisy Simulation

To start, we create a simple circuit that we want to simulate, the one generating the 4-qubit GHZ state $|\psi\rangle = \frac{1}{\sqrt{2}}(|0000\rangle + |1111\rangle)$. The code is as follows:

n_qubits = 4
circ = chain(
    put(n_qubits, 1 => H),
    [control(n_qubits, i, i+1 => X) for i in 1:n_qubits-1]...,
)
nqubits: 4
chain
├─ put on (1)
│  └─ H
├─ control(1)
│  └─ (2,) X
├─ control(2)
│  └─ (3,) X
└─ control(3)
   └─ (4,) X

Visualize the circuit

vizcircuit(circ)

The ideal simulation gives the following result:

reg = zero_state(n_qubits) |> circ
samples = measure(reg, nshots=1000);

Visualize the results

hist(map(x -> x.buf, samples))

Add errors to noise model

circ_noisy = Optimise.replace_block(circ) do x
    if x isa PutBlock && length(x.locs) == 1
        chain(x, put(nqubits(x), x.locs => quantum_channel(BitFlipError(0.1))))
    elseif x isa ControlBlock && length(x.ctrl_locs) == 1 && length(x.locs) == 1
        chain(x, put(nqubits(x), (x.ctrl_locs..., x.locs...) => kron(quantum_channel(BitFlipError(0.1)), quantum_channel(BitFlipError(0.1)))))
    else
        x
    end
end

push!(circ_noisy, repeat(quantum_channel(BitFlipError(0.05)), n_qubits)) # add measurement noise
nqubits: 4
chain
├─ chain
│  ├─ put on (1)
│  │  └─ H
│  └─ put on (1)
│     └─ mixed_unitary_channel
│        ├─ [0.9] I2
│        └─ [0.1] X
├─ chain
│  ├─ control(1)
│  │  └─ (2,) X
│  └─ put on (1, 2)
│     └─ mixed_unitary_channel
│        ├─ [0.81] kron
│        │  ├─ 1=>I2
│        │  └─ 2=>I2
│        ├─ [0.09000000000000001] kron
│        │  ├─ 1=>I2
│        │  └─ 2=>X
│        ├─ [0.09000000000000001] kron
│        │  ├─ 1=>X
│        │  └─ 2=>I2
│        └─ [0.010000000000000002] kron
│           ├─ 1=>X
│           └─ 2=>X
├─ chain
│  ├─ control(2)
│  │  └─ (3,) X
│  └─ put on (2, 3)
│     └─ mixed_unitary_channel
│        ├─ [0.81] kron
│        │  ├─ 1=>I2
│        │  └─ 2=>I2
│        ├─ [0.09000000000000001] kron
│        │  ├─ 1=>I2
│        │  └─ 2=>X
│        ├─ [0.09000000000000001] kron
│        │  ├─ 1=>X
│        │  └─ 2=>I2
│        └─ [0.010000000000000002] kron
│           ├─ 1=>X
│           └─ 2=>X
├─ chain
│  ├─ control(3)
│  │  └─ (4,) X
│  └─ put on (3, 4)
│     └─ mixed_unitary_channel
│        ├─ [0.81] kron
│        │  ├─ 1=>I2
│        │  └─ 2=>I2
│        ├─ [0.09000000000000001] kron
│        │  ├─ 1=>I2
│        │  └─ 2=>X
│        ├─ [0.09000000000000001] kron
│        │  ├─ 1=>X
│        │  └─ 2=>I2
│        └─ [0.010000000000000002] kron
│           ├─ 1=>X
│           └─ 2=>X
└─ repeat on (4)
   └─ mixed_unitary_channel
      ├─ [0.95] I2
      └─ [0.05] X

simulate the noisy circuit

rho = apply(density_matrix(reg), circ_noisy)
samples = measure(rho, nshots=1000);

Visualize the results

hist(map(x -> x.buf, samples))

This page was generated using Literate.jl.