Modular, production-grade implementation of Quantum Bayesian Classifiers using MindQuantum
Based on the research paper:
"Quantum Bayes classifiers and their application in image classification"
Ming-Ming Wang & Xiao-Ying Zhang
arXiv:2401.01588v2 [quant-ph]
- Naive QBC: Independence assumption - P(X|y) = ∏ P(xᵢ|y)
- SPODE QBC: Super-Parent One-Dependent Estimator with central attribute as super-parent
- TAN QBC: Tree-Augmented Naive Bayes using maximum spanning tree
- Symmetric QBC: Exploits symmetric relationships in image features
- Data Loading: MNIST & Fashion-MNIST support
- Local Feature Sampling: 3×3 grid with 7×7 blocks (9 attributes)
- Gaussian Binarization: MLE-based intersection method (Eqs. 19-21)
- Bayesian Statistics: Automated computation of P(y) and P(xᵢ|parents)
- Quantum Circuits: Amplitude encoding with f(P) = 2·arccos(√P)
- Inference: Statevector simulation with probability extraction
- Evaluation: Accuracy, Precision, Recall, F1-Score, Confusion Matrix
- Sample images & labels
- Sampling grid overlay
- Binary feature heatmaps
- Gaussian distribution plots
- Confusion matrices
- Performance comparisons
- Bayesian network graphs
- Prediction examples (correct/incorrect)
- Real-time experimentation
- Dataset selection (MNIST/Fashion-MNIST)
- Binary classification configurator
- Multiple QBC comparison
- Comprehensive result dashboard
- Publication-quality plots
- Python 3.9 or higher
- pip or conda
cd ~/qml
# Project already exists in qbc_project/python3 -m venv qbc_env
source qbc_env/bin/activate # On Windows: qbc_env\Scripts\activatecd qbc_project
pip install -r requirements.txtNote: If MindQuantum installation fails, try:
pip install mindquantum --index-url https://pypi.org/simplecd qbc_project
streamlit run streamlit_app.pyThen open your browser at http://localhost:8501
from preprocessing import DataLoader, ImagePreprocessor
from bayesian_stats import NaiveBayesStats
from quantum_circuits import build_qbc_circuit
from inference import QBCInference, PerformanceEvaluator
# 1. Load data
loader = DataLoader('mnist_784')
X, y = loader.load_data()
# 2. Preprocess
preprocessor = ImagePreprocessor(block_size=7, grid_size=3)
X_train_binary, y_train = preprocessor.fit_transform(X[:1000], y[:1000], class0=0, class1=1)
X_test_binary = preprocessor.transform(X[1000:1200])
y_test = y[1000:1200]
# 3. Compute statistics (Naive Bayes)
stats = NaiveBayesStats()
stats.fit(X_train_binary, y_train, [0, 1])
# 4. Build quantum circuit
circuit, builder = build_qbc_circuit('naive', n_attributes=9, statistics=stats)
print(f"Circuit: {len(circuit)} gates, depth {circuit.depth()}")
# 5. Inference
inference = QBCInference(circuit, n_attributes=9)
predictions, probabilities = inference.predict(X_test_binary)
# 6. Evaluate
evaluator = PerformanceEvaluator()
metrics = evaluator.compute_metrics(y_test, predictions, class0=0, class1=1)
evaluator.print_metrics(metrics, 0, 1)qbc_project/
├── preprocessing.py # Data loading, sampling, binarization
├── bayesian_stats.py # Bayesian statistics for all structures
├── quantum_circuits.py # MindQuantum circuit builders
├── inference.py # Prediction & evaluation
├── visualization.py # Matplotlib & Plotly visualizations
├── streamlit_app.py # Interactive web interface
├── requirements.txt # Python dependencies
├── __init__.py # Package initialization
└── README.md # This file
Classes:
DataLoader: Fetch MNIST/Fashion-MNIST from OpenMLLocalFeatureSampler: Extract 3×3 grid of 7×7 blocks (9 attributes)GaussianBinarizer: MLE-based binarization using Gaussian intersectionImagePreprocessor: Complete pipeline (sampling → binarization)
Key Methods:
_find_intersections(): Solve Gaussian intersection quadratic equation_binarize_value(): Apply Eqs. 19-21 from paperfit_transform(): One-step preprocessing
Classes:
NaiveBayesStats: Compute P(y) and P(xᵢ|y)SPODEStats: Compute P(y), P(x_super|y), P(xᵢ|y, x_super)TANStats: Build maximum spanning tree using CMI, compute P(xᵢ|y, x_parent)SymmetricStats: Use symmetric pairs for structure
Key Methods:
compute_prior(): P(y) with Laplace smoothing_compute_conditional_mutual_info(): I(xᵢ, xⱼ|y) for TAN_build_maximum_spanning_tree(): Prim's algorithmget_probabilities(): Return fitted statistics
Classes:
NaiveQBC: Build Naive quantum circuitSPODE_QBC: Build SPODE circuit with 2-control gatesTAN_QBC: Build TAN circuit layer-by-layerSymmetricQBC: Build symmetric circuit
Key Functions:
f_angle(p): f(P) = 2·arccos(√P) encoding_apply_controlled_ry_on_state(): Apply CRy with specific control statesbuild_qbc_circuit(): Factory function
Circuit Construction:
- Qubit 0: Label (y)
- Qubits 1-n: Attributes (x₁...xₙ)
- RY gates for probability encoding
- Controlled-RY for dependencies
- X gates for control state flipping
Classes:
QBCInference: Run circuits and extract predictionsPerformanceEvaluator: Compute comprehensive metrics
Key Methods:
predict_single(): Get P(y=0, X*) and P(y=1, X*) from statevectorpredict(): Batch predictioncompute_metrics(): Accuracy, Precision, Recall, F1, CMcompare_classifiers(): Side-by-side comparison
Prediction Process:
- Apply circuit to get statevector
- Find basis states matching test feature X*
- Extract amplitudes → probabilities
- Choose class with max P(y, X*)
Class: QBCVisualizer
Methods:
plot_sample_images(): Grid of images with labelsplot_sampling_grid(): Overlay sampling blocks on imageplot_binary_features(): Heatmap of binary vectorsplot_gaussian_distributions(): Gaussian PDFs and intersectionsplot_confusion_matrix(): Annotated CM with accuracyplot_metrics_comparison(): Bar chart comparing QBCsplot_interactive_comparison(): Plotly interactive chartplot_prediction_examples(): Show correct/incorrect predictionsplot_bayesian_network(): Visualize network structure
Styles:
- Research-quality plots for papers
- Color-coded (green=correct, red=incorrect)
- Professional fonts and layouts
- Seaborn + Matplotlib + Plotly
streamlit run streamlit_app.py
# In sidebar:
# - Dataset: MNIST
# - Class 0: 0, Class 1: 1
# - QBC Types: All
# - Training: 500, Test: 200
# Click "Run Experiment"Expected Results:
- Naive: ~99.2% accuracy
- SPODE: ~99.0%
- TAN: ~96.8%
- Symmetric: ~99.4%
# In sidebar:
# - Dataset: Fashion-MNIST
# - Class 0: 0, Class 1: 1
# - QBC Types: Naive, SPODEExpected Results:
- Naive: ~84-86%
- SPODE: ~86-89%
from preprocessing import *
from bayesian_stats import *
from quantum_circuits import *
from inference import *
from visualization import *
# Load & preprocess
loader = DataLoader('mnist_784')
X, y = loader.load_data()
preprocessor = ImagePreprocessor()
X_train_bin, y_train = preprocessor.fit_transform(X[:2000], y[:2000], 0, 1)
X_test_bin = preprocessor.transform(X[2000:2400])
y_test = y[2000:2400]
results = {}
# Naive
stats_naive = NaiveBayesStats()
stats_naive.fit(X_train_bin, y_train, [0, 1])
circuit_naive, _ = build_qbc_circuit('naive', 9, stats_naive)
inf_naive = QBCInference(circuit_naive, 9)
pred_naive, _ = inf_naive.predict(X_test_bin)
results['Naive'] = PerformanceEvaluator.compute_metrics(y_test, pred_naive, 0, 1)
# SPODE
stats_spode = SPODEStats(super_parent_idx=4)
stats_spode.fit(X_train_bin, y_train, [0, 1])
circuit_spode, _ = build_qbc_circuit('spode', 9, stats_spode, super_parent_idx=4)
inf_spode = QBCInference(circuit_spode, 9)
pred_spode, _ = inf_spode.predict(X_test_bin)
results['SPODE'] = PerformanceEvaluator.compute_metrics(y_test, pred_spode, 0, 1)
# TAN
stats_tan = TANStats()
stats_tan.fit(X_train_bin, y_train, [0, 1])
circuit_tan, _ = build_qbc_circuit('tan', 9, stats_tan)
inf_tan = QBCInference(circuit_tan, 9)
pred_tan, _ = inf_tan.predict(X_test_bin)
results['TAN'] = PerformanceEvaluator.compute_metrics(y_test, pred_tan, 0, 1)
# Compare
PerformanceEvaluator.compare_classifiers(results)
# Visualize
viz = QBCVisualizer()
fig = viz.plot_metrics_comparison(results, title="QBC Comparison: MNIST 0 vs 1")
fig.savefig('qbc_comparison.png', dpi=300, bbox_inches='tight')To reproduce Table I from the paper (45 binary classification pairs):
import numpy as np
from itertools import combinations
dataset = 'mnist_784'
results_all = {}
for class0, class1 in combinations(range(10), 2):
print(f"\nClassifying {class0} vs {class1}...")
# Load & preprocess
loader = DataLoader(dataset)
X, y = loader.load_data()
preprocessor = ImagePreprocessor()
X_train_bin, y_train = preprocessor.fit_transform(X[:5000], y[:5000], class0, class1)
X_test_bin = preprocessor.transform(X[5000:6000])
y_test = y[5000:6000]
y_test = y_test[(y_test == class0) | (y_test == class1)]
X_test_bin = X_test_bin[:(len(y_test))]
# Train Naive QBC
stats = NaiveBayesStats()
stats.fit(X_train_bin, y_train, [class0, class1])
circuit, _ = build_qbc_circuit('naive', 9, stats)
inf = QBCInference(circuit, 9)
pred, _ = inf.predict(X_test_bin)
metrics = PerformanceEvaluator.compute_metrics(y_test, pred, class0, class1)
results_all[f"{class0}_vs_{class1}"] = metrics['accuracy']
# Compute statistics
avg_acc = np.mean(list(results_all.values()))
variance = np.var(list(results_all.values()))
print(f"\nAverage Accuracy: {avg_acc:.4f}")
print(f"Variance: {variance:.4f}")The paper uses f(P) = 2·arccos(√P) to encode probabilities into rotation angles.
Why this works:
- After RY(θ) on |0⟩: cos²(θ/2)|0⟩ + sin²(θ/2)|1⟩
- Want: cos²(θ/2) = P(x=0)
- Solve: θ/2 = arccos(√P) → θ = 2·arccos(√P)
Uses Gaussian MLE intersection method (Eqs. 19-21):
- Fit Gaussian N(μ₀, σ₀²) and N(μ₁, σ₁²) for two classes
- Find intersection points by solving quadratic equation
- Apply rules based on number of intersections (0, 1, or 2)
| QBC Type | # Qubits | # Gates | Depth |
|---|---|---|---|
| Naive | 10 | ~37 | ~19 |
| SPODE | 10 | ~73 | ~37 |
| TAN | 10 | ~65-73 | ~33-37 |
| Symmetric | 10 | ~50-65 | ~25-33 |
pip install mindquantum==0.9.0
# Or try:
conda install mindquantum -c mindspore -c conda-forgepip install --upgrade scikit-learn scipy numpystreamlit run streamlit_app.py --server.port 8502Reduce n_train_samples and n_test_samples in Streamlit sidebar.
-
Original Paper:
Wang, M.-M., & Zhang, X.-Y. (2024). Quantum Bayes classifiers and their application in image classification. arXiv preprint arXiv:2401.01588v2. -
MindQuantum Documentation:
https://www.mindspore.cn/mindquantum/docs/en/master/index.html -
Datasets:
- MNIST: http://yann.lecun.com/exdb/mnist/
- Fashion-MNIST: https://github.com/zalandoresearch/fashion-mnist
-
Bayesian Networks:
- Zhou, Z.-H. (2021). Machine Learning. Springer Nature.
Contributions welcome! Areas for improvement:
- Support for multi-class classification (one-vs-rest)
- Real quantum device execution (IBM, IonQ)
- Circuit optimization (gate reduction)
- More datasets (CIFAR-10, ImageNet)
- Hyperparameter tuning interface
MIT License - see LICENSE file
- Original paper authors: Ming-Ming Wang & Xiao-Ying Zhang
- MindQuantum development team
- OpenML for dataset hosting
- Streamlit for amazing web framework
Made with ❤️ for Quantum Machine Learning Research