Upload 9 files
Browse files- X_test.npy +3 -0
- X_train.npy +3 -0
- circuit.qpy +0 -0
- config.json +21 -0
- qnn_inference.py +117 -0
- requirements.txt +8 -0
- weights.npy +3 -0
- y_test.npy +3 -0
- y_train.npy +3 -0
X_test.npy
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:2f86f0040e6db07589dbb41db72f387f3e0fe8af5b7ef51373085ac8e56d68e1
|
| 3 |
+
size 192
|
X_train.npy
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:d97e1c30a771fa45d3b39f84afbb0d64861051fb53f0db0bca7037984b5dc883
|
| 3 |
+
size 256
|
circuit.qpy
ADDED
|
Binary file (712 Bytes). View file
|
|
|
config.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"model_type": "quantum_neural_network",
|
| 3 |
+
"architecture": "hardware_efficient_ansatz",
|
| 4 |
+
"num_qubits": 2,
|
| 5 |
+
"num_parameters": 6,
|
| 6 |
+
"trainable_parameters": 4,
|
| 7 |
+
"circuit_depth": 4,
|
| 8 |
+
"backend": "ibm_fez",
|
| 9 |
+
"task": "binary_classification",
|
| 10 |
+
"dataset": "two_moons",
|
| 11 |
+
"train_samples": 8,
|
| 12 |
+
"test_samples": 4,
|
| 13 |
+
"qiskit_version": "1.0.0",
|
| 14 |
+
"framework": "qiskit",
|
| 15 |
+
"license": "apache-2.0",
|
| 16 |
+
"creation_date": "2025-12-06",
|
| 17 |
+
"gates": {
|
| 18 |
+
"ry": 6,
|
| 19 |
+
"cx": 1
|
| 20 |
+
}
|
| 21 |
+
}
|
qnn_inference.py
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
from qiskit import qpy
|
| 3 |
+
from qiskit.circuit import ParameterVector
|
| 4 |
+
from qiskit_machine_learning.neural_networks import SamplerQNN
|
| 5 |
+
from qiskit_machine_learning.algorithms.classifiers import NeuralNetworkClassifier
|
| 6 |
+
from qiskit.primitives import Sampler
|
| 7 |
+
import numpy as np
|
| 8 |
+
from huggingface_hub import hf_hub_download
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def load_qnn_model(repo_id="squ11z1/Two-Moons"):
|
| 12 |
+
|
| 13 |
+
# Download files
|
| 14 |
+
circuit_path = hf_hub_download(repo_id=repo_id, filename="circuit.qpy")
|
| 15 |
+
weights_path = hf_hub_download(repo_id=repo_id, filename="weights.npy")
|
| 16 |
+
|
| 17 |
+
# Load circuit
|
| 18 |
+
with open(circuit_path, 'rb') as f:
|
| 19 |
+
circuit = qpy.load(f)[0]
|
| 20 |
+
|
| 21 |
+
# Load weights
|
| 22 |
+
weights = np.load(weights_path)
|
| 23 |
+
|
| 24 |
+
return circuit, weights
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def create_qnn_classifier(circuit, weights):
|
| 28 |
+
|
| 29 |
+
# Get parameters from circuit
|
| 30 |
+
input_params = [p for p in circuit.parameters if p.name.startswith('x')]
|
| 31 |
+
weight_params = [p for p in circuit.parameters if p.name.startswith('w')]
|
| 32 |
+
|
| 33 |
+
# Parity interpretation function
|
| 34 |
+
def parity(x):
|
| 35 |
+
# Convert to binary string and count ones
|
| 36 |
+
return bin(x).count("1") % 2
|
| 37 |
+
|
| 38 |
+
# Create sampler (using V2 to avoid deprecation)
|
| 39 |
+
from qiskit.primitives import StatevectorSampler as Sampler
|
| 40 |
+
|
| 41 |
+
sampler = Sampler()
|
| 42 |
+
|
| 43 |
+
# Create QNN
|
| 44 |
+
qnn = SamplerQNN(
|
| 45 |
+
circuit=circuit,
|
| 46 |
+
input_params=input_params,
|
| 47 |
+
weight_params=weight_params,
|
| 48 |
+
interpret=parity,
|
| 49 |
+
output_shape=2,
|
| 50 |
+
sampler=sampler
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
# Create classifier
|
| 54 |
+
classifier = NeuralNetworkClassifier(
|
| 55 |
+
neural_network=qnn,
|
| 56 |
+
optimizer=None # Pre-trained
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
# Set weights
|
| 60 |
+
classifier._fit_result = type('obj', (object,), {'x': weights})
|
| 61 |
+
|
| 62 |
+
return classifier
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def predict(X, repo_id="squ11z1/Two-Moons"):
|
| 66 |
+
|
| 67 |
+
# Load model
|
| 68 |
+
circuit, weights = load_qnn_model(repo_id)
|
| 69 |
+
|
| 70 |
+
# Create classifier
|
| 71 |
+
classifier = create_qnn_classifier(circuit, weights)
|
| 72 |
+
|
| 73 |
+
# Predict
|
| 74 |
+
predictions = classifier.predict(X)
|
| 75 |
+
|
| 76 |
+
return predictions
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
# Example usage
|
| 80 |
+
if __name__ == "__main__":
|
| 81 |
+
print("="*70)
|
| 82 |
+
print("Quantum Neural Network - Inference Example")
|
| 83 |
+
print("="*70)
|
| 84 |
+
|
| 85 |
+
# Example: Load and predict
|
| 86 |
+
repo_id = "squ11z1/Two-Moons"
|
| 87 |
+
|
| 88 |
+
print("\n1. Loading model from Hugging Face...")
|
| 89 |
+
circuit, weights = load_qnn_model(repo_id)
|
| 90 |
+
print(f"Circuit: {circuit.num_qubits} qubits, depth {circuit.depth()}")
|
| 91 |
+
print(f"Weights: {weights}")
|
| 92 |
+
|
| 93 |
+
print("\n2. Creating classifier...")
|
| 94 |
+
classifier = create_qnn_classifier(circuit, weights)
|
| 95 |
+
print(f" Classifier ready")
|
| 96 |
+
|
| 97 |
+
print("\n3. Making predictions...")
|
| 98 |
+
# Example data (Two Moons-like points)
|
| 99 |
+
X_test = np.array([
|
| 100 |
+
[0.5, 0.2], # Class 1
|
| 101 |
+
[-0.5, 0.5], # Class 0
|
| 102 |
+
[1.0, 0.0], # Class 1
|
| 103 |
+
[-1.0, 0.8] # Class 0
|
| 104 |
+
])
|
| 105 |
+
|
| 106 |
+
predictions = classifier.predict(X_test)
|
| 107 |
+
|
| 108 |
+
print(f"\n Input data:")
|
| 109 |
+
for i, x in enumerate(X_test):
|
| 110 |
+
print(f" Sample {i+1}: {x}")
|
| 111 |
+
|
| 112 |
+
print(f"\n Predictions: {predictions}")
|
| 113 |
+
print(f" (0 = Negative class, 1 = Positive class)")
|
| 114 |
+
|
| 115 |
+
print("\n" + "="*70)
|
| 116 |
+
print("Inference complete!")
|
| 117 |
+
print("="*70)
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
qiskit>=1.0.0
|
| 2 |
+
qiskit-aer>=0.13.0
|
| 3 |
+
qiskit-machine-learning>=0.7.0
|
| 4 |
+
qiskit-algorithms>=0.3.0
|
| 5 |
+
numpy>=1.24.0
|
| 6 |
+
scikit-learn>=1.3.0
|
| 7 |
+
matplotlib>=3.7.0
|
| 8 |
+
huggingface-hub>=0.20.0
|
weights.npy
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:0b86c9f8617fbe53406f0e4699496a4e84fb32882fe283abc5c4e73770a1eb04
|
| 3 |
+
size 160
|
y_test.npy
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:80fbb199e82fb38f1f2d14b378fb2c24423b540848437f63b8572eea4c23c0e3
|
| 3 |
+
size 160
|
y_train.npy
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:63422c1230165e6e7b67b1b0f3722075662908438eb34a1be9e25f6c9e975e6f
|
| 3 |
+
size 192
|