Quantum Bayesian Network Sampling
pyagrum.qBNSampling provides a quantum-circuit encoding of Bayesian networks
and a quantum rejection-sampling inference engine, built on top of
Qiskit and the Aer simulator.
The module contains two classes:
qBNMC— encodes aBayesNetas a quantum circuit so that measuring the circuit samples from the network’s joint distribution.qBNRejection— runs quantum rejection sampling on top of aqBNMCcircuit to compute posterior distributions conditioned on evidence.
import pyagrum as gum
import pyagrum.qBNSampling as qBNS
bn = gum.loadBN("asia.bif")
# Build the quantum circuit encoding
qbn = qBNS.qBNMC(bn)
marginals = qbn.runBN(shots=10000) # dict[str, Tensor]
# Quantum rejection-sampling inference
ie = qBNS.qBNRejection(qbn)
ie.setEvidence({"dyspnoea": 1})
ie.makeInference()
print(ie.posterior("bronchitis"))
qBNMC — Circuit encoding
Based on Borujeni et al. [BNN+21].
Each variable in the Bayesian network is mapped to ⌈log₂(domainSize)⌉ qubits (Section 3.3 of the paper). The CPT of each node is encoded via multi-qubit RY rotations (Section 3.2, Fig. 5):
root nodes receive unconditional rotations;
non-root nodes receive controlled rotations, one block per parent configuration, with X-gate framing to select the correct control state.
Measuring all qubits of the resulting circuit samples from the joint distribution of the network.
- class pyagrum.qBNSampling.qBNMC(bn)
Bases:
objectQuantum circuit representation of a Bayesian network.
Encodes a pyAgrum BayesNet into a quantum circuit using multi-qubit RY rotations so that measuring the circuit samples from the joint distribution of the network.
- Parameters:
bn (
BayesNet) – pyAgrum Bayesian network to encode.
- bn
The encoded Bayesian network.
- Type:
BayesNet
- n_qb_map
Maps each node ID to the list of qubit IDs assigned to it.
- Type:
dict[int,list[int]]
Scientific notes
qBNMC encodes a Bayesian network as a quantum circuit, following [1], so that measuring all the qubits draws a sample of the joint distribution. A variable with \(d\) values is represented by \(\lceil \log_2 d \rceil\) qubits, a value by its binary code (the codes beyond \(d - 1\) get a null probability). The probabilities are loaded by rotations: the gate \(R_Y(\theta)\) maps \(|0\rangle\) to \(\cos(\theta/2)|0\rangle + \sin(\theta/2)|1\rangle\), so a qubit gets the probabilities \(P_0, P_1\) with
The variables are encoded in a topological order. The first qubit of a variable is rotated with the probability of its bit, each following qubit by rotations controlled by the preceding ones, with the conditional probability of its bit given theirs. A variable with parents receives one such block per configuration of its parents, controlled by the qubits of the parents (X gates around the block select the configuration). The circuit therefore has a number of controlled rotations proportional to the size of the conditional probability tables.
runBN builds and measures the circuit shots times (10000 by default) on the Aer simulator of Qiskit, and the
marginals are the frequencies of the values among the shots: a Monte Carlo estimate, whose precision depends on the
number of shots (see pyagrum.MonteCarloSampling for the classical forward sampling it reproduces). The shots are
seeded by the random generator of pyAgrum. Implementation limit. The circuit is simulated on a classical computer,
whose cost grows exponentially with the total number of qubits: only small networks can be handled.
Methods
Run a circuit on the Aer simulator and return marginal probabilities. |
|
Build the quantum circuit encoding the Bayesian network. |
|
All parent-state combinations for a given node. |
|
Binary-encode variable states. |
|
Conditional probability for one qubit given context (eq18/eq20). |
|
Quantum registers for the Bayesian network circuit. |
|
IDs of root nodes (no parents) in the DAG. |
|
Total number of qubits required for the full circuit. |
|
Number of qubits needed to represent a variable. |
|
Match binary strings against target conditions (eq17/eq19). |
|
Map node IDs to lists of qubit IDs. |
|
Add multi-qubit RY rotations to the circuit (Fig9/eq18). |
|
Build and run the circuit; return marginals as Tensors. |
qBNRejection — Quantum inference
Based on Low et al. [LYC14].
Inference is performed via quantum rejection sampling with Grover-based amplitude amplification (Algorithm 1 of the paper). The key operators are:
A — the sample-preparation circuit (the
qBNMCcircuit without measurement).G = S_e A⁻¹ S₀ A — the Grover iterate, where S_e is a phase flip on the evidence qubits and S₀ is a phase flip on the all-zero state.
Each call to getSample() runs
Algorithm 1: it applies G^{⌈2^k⌉} for increasing k until a measurement
consistent with the evidence is obtained.
- class pyagrum.qBNSampling.qBNRejection(qbn)
Bases:
objectQuantum rejection-sampling inference on a Bayesian network.
Implements the quantum rejection-sampling algorithm to compute posterior distributions conditioned on evidence, using a quantum circuit encoding of the Bayesian network (Grover-based amplitude amplification).
- q_registers
Quantum registers used to build rotation gates.
- Type:
dict[int,QuantumRegister]
- evidence
Current evidence: variable name → state index.
- Type:
dict[int | str,int]
- max_iter
Maximum number of rejection-sampling iterations (default 1000).
- Type:
int
Scientific notes
qBNRejection implements the quantum rejection sampling of [2] on the circuit of
pyagrum.qBNSampling.qBNMC. Classical rejection sampling draws samples of the network and keeps those that agree
with the evidence \(e\), which needs about \(1/P(e)\) draws per kept sample. Its quantum version amplifies the
amplitude of the states that agree with the evidence before measuring, with the Grover iterate
where \(A\) is the circuit of pyagrum.qBNSampling.qBNMC (without measurement), \(S_e\) flips the phase of the
states that agree with the evidence and \(S_0\) flips the phase of the all-zero state (the global sign does not
change the measurements). One sample is drawn by Algorithm 1 of [2]: for \(k = 0, 1, 2, \dots\)
the state \(A|0\rangle\) is prepared, \(G^{2^k}\) is applied, and all the qubits are measured, until the
measured values of the evidence variables are the observed ones; the measured state is then an exact sample of
\(P(X \mid e)\). This needs \(O(P(e)^{-1/2})\) applications of \(G\) per sample instead of
\(O(P(e)^{-1})\) draws [2]. pyAgrum stops with an error when \(k\) exceeds 21 (evidence
impossible or almost).
makeInference draws maxIter samples (1000 by default) and the posterior of each variable is the frequency of its
values among them; the evidence is a hard evidence (one value per observed variable). useFragmentBN restricts the
circuit to the ancestors of the targets and of the evidence, the other variables having no effect on these
posteriors, which reduces the number of qubits. Implementation limit. As for pyagrum.qBNSampling.qBNMC, the
circuits are simulated on a classical computer: the speedup is the one of the number of operations of the algorithm,
not a gain of time, and the cost grows exponentially with the number of qubits.
Guang Hao Low, Theodore J. Yoder, and Isaac L. Chuang. Quantum inference on Bayesian networks. Physical Review A, 89(6):062315, 2014. URL: https://arxiv.org/abs/1402.7359, doi:10.1103/PhysRevA.89.062315.
Methods
Compose the sample-preparation operator A onto the circuit. |
|
Apply X gates to evidence qubits with state 0 (eq7 phase flip helper). |
|
Compose one Grover iterate G = S_e A^{-1} S_0 A onto the circuit. |
|
Compose the adjoint of M onto the circuit. |
|
Apply the phase-flip operator S = B Z B† (eq7). |
|
Apply a (multi-controlled) Z gate over evidence qubits (eq7). |
|
Convert node-level evidence to qubit-level evidence. |
|
Precompute and cache operators A and G for the current evidence. |
|
Draw one sample consistent with evidence using Algorithm 1. |
|
Run rejection sampling and accumulate marginal probabilities. |
|
Current maximum iteration count. |
|
Return the posterior distribution of a variable as a Tensor. |
|
Set the evidence for subsequent inference calls. |
|
Set the maximum number of rejection-sampling iterations. |
|
Transpile cached A and G to optimisation level 3. |
|
Restrict the network to the ancestors of target and evidence nodes. |
Sima E. Borujeni, Saideep Nannapaneni, Nam H. Nguyen, Elizabeth C. Behrman, and James E. Steck. Quantum circuit representation of Bayesian networks. Expert Systems with Applications, 176:114768, 2021. URL: https://arxiv.org/abs/2004.14803, doi:10.1016/j.eswa.2021.114768.
Guang Hao Low, Theodore J. Yoder, and Isaac L. Chuang. Quantum inference on Bayesian networks. Physical Review A, 89(6):062315, 2014. URL: https://arxiv.org/abs/1402.7359, doi:10.1103/PhysRevA.89.062315.