Spaces:
Runtime error
Runtime error
File size: 8,139 Bytes
8da66fc | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | import asyncio
import pytest
import numpy as np
from cortex.quantum.quantum_processor import QuantumProcessor, Qubit
from cortex.quantum.entanglement import QuantumEntanglementManager, EntanglementType
from cortex.memory.quantum_memory import QuantumMemory
class TestQuantumProcessor:
"""Tests pour le processeur quantique"""
@pytest.fixture
async def quantum_processor(self):
"""Fixture pour initialiser le processeur quantique"""
processor = QuantumProcessor(qubit_count=10)
await processor.initialize()
return processor
@pytest.mark.asyncio
async def test_processor_initialization(self, quantum_processor):
"""Test l'initialisation du processeur quantique"""
assert quantum_processor is not None
assert len(quantum_processor.qubits) == 10
assert quantum_processor.gate_fidelity > 0.9
@pytest.mark.asyncio
async def test_superposition_creation(self, quantum_processor):
"""Test la création de superposition"""
qubit_ids = ["q000", "q001"]
result = await quantum_processor.create_superposition(qubit_ids)
assert result is True
for qid in qubit_ids:
qubit = quantum_processor.qubits[qid]
# Vérifie que le qubit est en superposition (état |+⟩)
expected_state = np.array([1/np.sqrt(2), 1/np.sqrt(2)])
np.testing.assert_array_almost_equal(qubit.state, expected_state)
@pytest.mark.asyncio
async def test_qubit_entanglement(self, quantum_processor):
"""Test l'intrication de qubits"""
result = await quantum_processor.entangle_qubits("q000", "q001")
assert result is True
assert "q001" in quantum_processor.qubits["q000"].entangled_with
assert "q000" in quantum_processor.qubits["q001"].entangled_with
@pytest.mark.asyncio
async def test_quantum_circuit_execution(self, quantum_processor):
"""Test l'exécution d'un circuit quantique"""
circuit = {
"id": "test_circuit",
"qubits": 5,
"gates": ["H", "CNOT", "X"],
"shots": 100
}
result = await quantum_processor.execute_quantum_circuit(circuit)
assert "results" in result
assert "probabilities" in result["results"]
assert result["execution_time"] is not None
assert result["fidelity"] > 0.9
@pytest.mark.asyncio
async def test_grover_search(self, quantum_processor):
"""Test l'algorithme de recherche de Grover"""
database = ["apple", "banana", "cherry", "date", "elderberry"]
target = "cherry"
result = await quantum_processor.grover_search(database, target)
assert "target_found" in result
assert result["speedup_factor"] > 1
assert result["quantum_complexity"] < result["classical_complexity"]
class TestQuantumEntanglement:
"""Tests pour le gestionnaire d'intrication"""
@pytest.fixture
async def entanglement_manager(self):
"""Fixture pour initialiser le gestionnaire d'intrication"""
manager = QuantumEntanglementManager()
await manager.initialize()
return manager
@pytest.mark.asyncio
async def test_bell_pair_creation(self, entanglement_manager):
"""Test la création de paires de Bell"""
pair = await entanglement_manager.create_bell_pair("q1", "q2")
assert pair.qubit_a == "q1"
assert pair.qubit_b == "q2"
assert pair.entanglement_type == EntanglementType.BELL_STATE
assert pair.fidelity > 0.9
assert len(entanglement_manager.entangled_pairs) == 1
@pytest.mark.asyncio
async def test_ghz_state_creation(self, entanglement_manager):
"""Test la création d'état GHZ"""
qubits = ["q1", "q2", "q3", "q4"]
result = await entanglement_manager.create_ghz_state(qubits)
assert result is True
assert len(entanglement_manager.entangled_groups) == 1
@pytest.mark.asyncio
async def test_entanglement_measurement(self, entanglement_manager):
"""Test la mesure d'intrication"""
await entanglement_manager.create_bell_pair("q1", "q2")
result = await entanglement_manager.measure_entanglement("q1", "q2")
assert "correlation" in result
assert result["entanglement_present"] is True
assert result["bell_inequality_violated"] is True
@pytest.mark.asyncio
async def test_quantum_teleportation(self, entanglement_manager):
"""Test la téléportation quantique"""
data = "Hello Quantum World!"
result = await entanglement_manager.quantum_teleportation("q1", "q2", data)
assert result["success"] is True
assert result["data_teleported"] == data
assert result["fidelity"] > 0.8
class TestQuantumMemory:
"""Tests pour la mémoire quantique"""
@pytest.fixture
async def quantum_memory(self):
"""Fixture pour initialiser la mémoire quantique"""
memory = QuantumMemory(capacity=100)
await memory.initialize()
return memory
@pytest.mark.asyncio
async def test_data_storage(self, quantum_memory):
"""Test le stockage de données quantiques"""
test_data = {"message": "Test quantum storage", "value": 42}
address = await quantum_memory.store_quantum_data(test_data)
assert address is not None
assert address in quantum_memory.memory_cells
assert quantum_memory.memory_cells[address].data == test_data
@pytest.mark.asyncio
async def test_data_retrieval(self, quantum_memory):
"""Test la récupération de données quantiques"""
test_data = "Quantum retrieval test"
address = await quantum_memory.store_quantum_data(test_data)
retrieved_data = await quantum_memory.retrieve_quantum_data(address)
assert retrieved_data == test_data
@pytest.mark.asyncio
async def test_memory_entanglement(self, quantum_memory):
"""Test l'intrication de cellules mémoire"""
addr1 = await quantum_memory.store_quantum_data("data1")
addr2 = await quantum_memory.store_quantum_data("data2")
result = await quantum_memory.entangle_memory_cells(addr1, addr2)
assert result is True
assert addr2 in quantum_memory.memory_cells[addr1].entangled_with
assert addr1 in quantum_memory.memory_cells[addr2].entangled_with
@pytest.mark.asyncio
async def test_superposition_storage(self, quantum_memory):
"""Test le stockage en superposition"""
data_list = ["state1", "state2", "state3"]
address = await quantum_memory.quantum_superposition_store(data_list)
assert address is not None
cell = quantum_memory.memory_cells[address]
assert cell.data == data_list
assert len(cell.quantum_state) == len(data_list)
@pytest.mark.asyncio
async def test_quantum_integration():
"""Test d'intégration des modules quantiques"""
# Initialisation des composants
processor = QuantumProcessor(5)
memory = QuantumMemory(50)
entanglement = QuantumEntanglementManager()
await processor.initialize()
await memory.initialize()
await entanglement.initialize()
# Test de workflow quantique complet
# 1. Stockage en mémoire
data = "Integration test data"
address = await memory.store_quantum_data(data)
# 2. Création d'intrication
await entanglement.create_bell_pair("q1", "q2")
# 3. Exécution de circuit
circuit = {"qubits": 3, "shots": 100}
result = await processor.execute_quantum_circuit(circuit)
# Vérifications
assert address is not None
assert len(entanglement.entangled_pairs) == 1
assert "results" in result
print("✅ Tests d'intégration quantique réussis!")
if __name__ == "__main__":
# Exécution des tests
asyncio.run(test_quantum_integration()) |