# Solving inference problem in quantum error correction with Turing.jl

**URL:** <https://discourse.julialang.org/t/solving-inference-problem-in-quantum-error-correction-with-turing-jl/129753>\
**Category:** Probabilistic Programming\
**Tags:** turing\
**Created:** [June 9, 2025, 6:12am UTC](https://discourse.julialang.org/t/solving-inference-problem-in-quantum-error-correction-with-turing-jl/129753 "2025-06-09T06:12:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nzy1997](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nzy1997/32/217257_2.png) [@nzy1997](https://discourse.julialang.org/u/nzy1997)\
**Post date:** [June 9, 2025, 6:12am UTC](https://discourse.julialang.org/t/solving-inference-problem-in-quantum-error-correction-with-turing-jl/129753/1 "2025-06-09T06:12:05Z")

</div>

Hi everyone,

I am trying to use Turing.jl to solve an inference problem in quantum error correction. Here is a simple problem.

**Problem Statement:**  
Consider a parity check bit c \in \{0,1\} computed from four binary variables x\_1, x\_2, x\_3, x\_4 \in \{0,1\} as follows:

c = x\_1 \oplus x\_2 \oplus x\_3 \oplus x\_4,

where \oplus denotes the XOR operation. Equivalently, c represents the parity (even or odd) of the sum x\_1 + x\_2 + x\_3 + x\_4.

Each bit x\_i is an independent Bernoulli random variable with success probability p\_i, i.e.,

P(x\_i = 1) = p\_i \quad \text{and} \quad P(x\_i = 0) = 1 - p\_i.

**Given the observed parity c = 1:**

1. Determine the **most probable configuration** of (x\_1, x\_2, x\_3, x\_4).
2. Find the **most probable value** of parity of a partial sum x\_1 + x\_2.

This is the **decoding problem** , which refers to the task of identifying the most likely error affecting a quantum state based on the observed **error syndrome**. The goal is to reverse the inferred error to recover the original logical state. Current approaches to solving this problem include belief propagation, integer programming, perfect matching, tensor networks, and other methods.

Here is my attempt.

```Julia
using Turing

@model function one_check(y)
    p1 ~ Bernoulli(0.15)
    p2 ~ Bernoulli(0.05)
    p3 ~ Bernoulli(0.05)
    p4 ~ Bernoulli(0.05)
    for i in eachindex(y)
        y[i] = p1 ⊻ p2 ⊻ p3 ⊻ p4
    end
end;

model = one_check([true])
map_estimate = maximum_a_posteriori(model)

mle_estimate = maximum_likelihood(model)

```

There are errors on the last two function, like

```julia
InexactError: Bool(NaN)

```

I don’t know how to solve this problem properly with Turing.jl.

Thanks!

---

<div class="post-metadata">

**Author:** ![eteppo](https://avatars.discourse-cdn.com/v4/letter/e/90db22/32.png) [@eteppo](https://discourse.julialang.org/u/eteppo)\
**Post date:** [June 9, 2025, 1:45pm UTC](https://discourse.julialang.org/t/solving-inference-problem-in-quantum-error-correction-with-turing-jl/129753/3 "2025-06-09T13:45:09Z")

</div>

Welcome! I can’t comment on the problem but three general things: 1) You need a likelihood for the observation, say `y != c ? Turing.@addlogprob! -Inf` which says that observing a mismatch is impossible. Now it’s inserting numbers to the input vector. 2) You can’t use any optimization or sampling methods that require continuous parameter spaces. For sampling you need to look for Gibbs, Metropolis-Hastings, etc. 3) Bernoulli uses 1 and 0 instead of true and false, watch out for that.

---

<div class="post-metadata">

**Author:** ![nzy1997](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nzy1997/32/217257_2.png) [@nzy1997](https://discourse.julialang.org/u/nzy1997)\
**Post date:** [June 11, 2025, 3:47am UTC](https://discourse.julialang.org/t/solving-inference-problem-in-quantum-error-correction-with-turing-jl/129753/4 "2025-06-11T03:47:41Z")

</div>

Thank you for your advises! I tried to apply 1) and 3) of them and I didn’t get your point in 2). Do you mean that `maximum_a_posteriori` and `maximum_likelihood` require continuous parameter spaces and Bernoulli distribution is not a continuous distribution?

The following is my second attempt. The error is similar.

```Julia
using Turing

@model function one_check()
    p1 ~ Bernoulli(0.15)
    p2 ~ Bernoulli(0.05)
    p3 ~ Bernoulli(0.05)
    p4 ~ Bernoulli(0.05)

    y ~ Bernoulli(0.5)
    if y != p1 ⊻ p2 ⊻ p3 ⊻ p4
        Turing.@addlogprob! -Inf
    end
    return nothing
end;

model = one_check() | (; y = 1)

map_estimate = maximum_a_posteriori(model)

```

---

<div class="post-metadata">

**Author:** ![ElOceanografo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eloceanografo/32/624_2.png) [@ElOceanografo](https://discourse.julialang.org/u/ElOceanografo)\
**Post date:** [June 11, 2025, 6:41am UTC](https://discourse.julialang.org/t/solving-inference-problem-in-quantum-error-correction-with-turing-jl/129753/5 "2025-06-11T06:41:54Z")

</div>

This problem doesn’t seem like the best fit for Turing, and definitely not for the MAP optimizer. The parameters you’re trying to infer are discrete, so none of the continuous or gradient-based optimizers will be able to do anything with them.

That’s the bad news. The good news is that the parameter space is finite and quite small (at least for n=4), so you can just do an exhaustive search:

```julia
using Distributions

function one_check(y, x)
    d1 = Bernoulli(0.15)
    d2 = Bernoulli(0.05)
    d3 = Bernoulli(0.05)
    d4 = Bernoulli(0.05)

    lp = sum(logpdf.([d1, d2, d3, d4], x))
    parity = isodd(sum(x))

    if any(yi -> isodd(yi) != parity, y)
        return -Inf
    else
        return lp
    end
end

y = [true]
tf = [true, false]
# generate all possible configurations of x
xx = collect(Iterators.product(tf, tf, tf, tf))
logprobs = [one_check(y, x) for x in xx]
lp, i = findmax(logprobs)
xmax = xx[i]

```

Here `xmax` gives your MAP estimate, and `exp.(logprobs)` is your full posterior distribution.

For higher-dimensional `x`, where the exhaustive search might become impractical, this problem would be a good fit for approximate Bayesian computation (ABC).

---

<div class="post-metadata">

**Author:** ![nzy1997](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nzy1997/32/217257_2.png) [@nzy1997](https://discourse.julialang.org/u/nzy1997)\
**Post date:** [June 11, 2025, 8:17am UTC](https://discourse.julialang.org/t/solving-inference-problem-in-quantum-error-correction-with-turing-jl/129753/6 "2025-06-11T08:17:54Z")

</div>

Thanks for the detailed explanation! The code example is very helpful for clarity. I’ll try this approach and go on to learn more about approximate Bayesian computation (ABC). Appreciate the guidance!
