https://github.com/matchcake/matchcake
Fermionic Machine Learning python package built on PennyLane
https://github.com/matchcake/matchcake
quantum-circuit-simulator quantum-computing quantum-machine-learning
Last synced: 23 days ago
JSON representation
Fermionic Machine Learning python package built on PennyLane
- Host: GitHub
- URL: https://github.com/matchcake/matchcake
- Owner: MatchCake
- License: apache-2.0
- Created: 2023-10-02T20:57:26.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2026-02-28T01:41:17.000Z (24 days ago)
- Last Synced: 2026-02-28T06:15:56.087Z (24 days ago)
- Topics: quantum-circuit-simulator, quantum-computing, quantum-machine-learning
- Language: Python
- Homepage: https://matchcake.github.io/MatchCake/
- Size: 8.39 MB
- Stars: 16
- Watchers: 1
- Forks: 2
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# MatchCake

[](https://github.com/MatchCake/MatchCake/stargazers)
[](https://github.com/MatchCake/MatchCake/network/members)
[](https://www.python.org/downloads/release/python-310/)
[](https://pypi.org/project/MatchCake)
[](https://pypi.org/project/MatchCake)
[](LICENSE)



[](https://github.com/psf/black)
[](https://codecov.io/github/MatchCake/MatchCake)
# Description
MatchCake is a Python package that provides a new PennyLane device for simulating a specific class of quantum
circuits called Matchgate circuits or matchcircuits. These circuits are made with matchgates, a class of restricted
quantum unitaries that are parity-preserving and operate on nearest-neighbor qubits. These constraints lead to
matchgates being classically simulable in polynomial time.
Additionally, this package provides quantum kernels made with [scikit-learn](https://scikit-learn.org/stable/) API allowing the
use matchcircuits as kernels in quantum machine learning algorithms. One way to use these kernels could be in a Support Vector
Machine (SVM).
Note that this package is built on PennyLane and PyTorch. This means that only the NumPy and PyTorch backends are compatible.
Other backends provided by Autoray, such as JAX and TensorFlow, are not supported.
We highly recommend using PyTorch as the backend when working with MatchCake.
# Installation
| Method | Commands |
|------------|----------------------------------------------------------|
| **poetry** | `poetry add matchcake` |
| **uv** | `uv add matchcake` |
| **PyPi** | `pip install MatchCake` |
| **source** | `pip install git+https://github.com/MatchCake/MatchCake` |
### Last unstable version
To install the latest unstable version, download the latest version from `https://github.com/MatchCake/MatchCake@dev`.
### CUDA installation
To use MatchCake with cuda, you can add `--extra cu128` to the installation commands above.
This will install pytorch with CUDA 12.8.
Extra options:
- `--extra cpu`: Install MatchCake with PyTorch CPU only.
- `--extra cu128`: Install MatchCake with PyTorch CUDA 12.8.
- `--extra cu130`: Install MatchCake with PyTorch CUDA 13.0.
# Quick Usage Preview
## Quantum Circuit Simulation with MatchCake
```python
import matchcake as mc
import pennylane as qml
import numpy as np
from pennylane.ops.qubit.observables import BasisStateProjector
# Create a Non-Interacting Fermionic Device
nif_device = mc.NonInteractingFermionicDevice(wires=4)
initial_state = np.zeros(len(nif_device.wires), dtype=int)
# Define a quantum circuit
def circuit(params, wires, initial_state=None):
qml.BasisState(initial_state, wires=wires)
for i, even_wire in enumerate(wires[:-1:2]):
idx = list(wires).index(even_wire)
curr_wires = [wires[idx], wires[idx + 1]]
mc.operations.CompRxRx(params, wires=curr_wires)
mc.operations.CompRyRy(params, wires=curr_wires)
mc.operations.CompRzRz(params, wires=curr_wires)
for i, odd_wire in enumerate(wires[1:-1:2]):
idx = list(wires).index(odd_wire)
mc.operations.fSWAP(wires=[wires[idx], wires[idx + 1]])
projector: BasisStateProjector = qml.Projector(initial_state, wires=wires)
return qml.expval(projector)
# Create a QNode
nif_qnode = qml.QNode(circuit, nif_device)
qml.draw_mpl(nif_qnode)(np.array([0.1, 0.2]), wires=nif_device.wires, initial_state=initial_state)
# Evaluate the QNode
expval = nif_qnode(np.random.random(2), wires=nif_device.wires, initial_state=initial_state)
print(f"Expectation value: {expval}")
```
## Data Classification with MatchCake
```python
from matchcake.ml.kernels import FermionicPQCKernel
from matchcake.ml.visualisation import ClassificationVisualizer
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
from sklearn.pipeline import Pipeline
from sklearn.svm import SVC
# Load the iris dataset
X, y = datasets.load_iris(return_X_y=True)
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
# Create and fit the model
pipeline = Pipeline([
('scaler', MinMaxScaler(feature_range=(0, 1))),
('kernel', FermionicPQCKernel(n_qubits=4, rotations="X,Z").freeze()),
('classifier', SVC(kernel='precomputed')),
])
pipeline.fit(x_train, y_train)
# Evaluate the model
test_accuracy = pipeline.score(x_test, y_test)
print(f"Test accuracy: {test_accuracy * 100:.2f}%")
# Visualize the classification
viz = ClassificationVisualizer(x=X, n_pts=1_000)
viz.plot_2d_decision_boundaries(model=pipeline, y=y, show=True)
```
# Tutorials
- [MatchCake Basics](https://github.com/MatchCake/MatchCake/blob/main/tutorials/matchcake_basics.ipynb)
- [Compute Expectation Values with MatchCake](https://github.com/MatchCake/MatchCake/blob/main/tutorials/expectation_values.ipynb)
- [Iris Classification with MatchCake](https://github.com/MatchCake/MatchCake/blob/main/tutorials/iris_classification.ipynb)
- [Nystroem Kernel Approximation](https://github.com/MatchCake/MatchCake/blob/main/tutorials/nystroem_kernel_approximation.ipynb)
# Notes
- This package is still in development and some features may not be available yet.
- The documentation is still in development and may not be complete yet.
# About
This work was supported by the Ministère de l'Économie, de l'Innovation et de l'Énergie du Québec
through its Research Chair in Quantum Computing, an NSERC Discovery grant, and the Canada First Research Excellence
Fund.
# Important Links
- Documentation at [https://MatchCake.github.io/MatchCake/](https://MatchCake.github.io/MatchCake/).
- Github at [https://github.com/MatchCake/MatchCake/](https://github.com/MatchCake/MatchCake/).
# Found a bug or have a feature request?
- [Click here to create a new issue.](https://github.com/MatchCake/MatchCake/issues/new/choose)
# License
[Apache License 2.0](LICENSE)
# Citation
Repository:
```
@misc{matchcake_Gince2023,
title={MatchCake},
author={Jérémie Gince},
year={2023},
publisher={Université de Sherbrooke},
url={https://github.com/MatchCake/MatchCake},
}
```
## Fermionic Machine Learning Paper
[Fermionic Machine Learning](https://ieeexplore.ieee.org/document/10821385) is a work presented at the 2024 IEEE
International Conference on Quantum Computing and Engineering (QCE). The paper compares unconstrained quantum kernel
methods with constraint-based kernels derived from matchgate (free-fermionic) circuits, and benchmarks their
performance on supervised classification tasks. All free-fermionic kernels considered in this work were simulated
using MatchCake.
[IEEE Xplore paper](https://ieeexplore.ieee.org/document/10821385):
```
@INPROCEEDINGS{10821385,
author={Gince, Jérémie and Pagé, Jean-Michel and Armenta, Marco and Sarkar, Ayana and Kourtis, Stefanos},
booktitle={2024 IEEE International Conference on Quantum Computing and Engineering (QCE)},
title={Fermionic Machine Learning},
year={2024},
volume={01},
number={},
pages={1672-1678},
keywords={Runtime;Quantum entanglement;Computational modeling;Benchmark testing;Rendering (computer graphics);Hardware;Kernel;Integrated circuit modeling;Quantum circuit;Standards;Quantum machine learning;quantum kernel methods;matchgate circuits;fermionic quantum computation;data classification},
doi={10.1109/QCE60285.2024.00195}
}
```
[ArXiv paper](https://arxiv.org/abs/2404.19032):
```
@misc{gince2024fermionic,
title={Fermionic Machine Learning},
author={Jérémie Gince and Jean-Michel Pagé and Marco Armenta and Ayana Sarkar and Stefanos Kourtis},
year={2024},
eprint={2404.19032},
archivePrefix={arXiv},
primaryClass={quant-ph}
}
```