import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'py', 'python'))
def test_quantrs2_core_integration():
print("🚀 Testing QuantRS2 Core Integration as quantrs2.core")
print("=" * 60)
try:
import quantrs2.core as core
print("✅ Successfully imported quantrs2.core")
q0 = core.QubitId(0)
q1 = core.QubitId(1)
print(f"✅ Created QubitIds: {q0}, {q1}")
gates_created = []
h_gate = core.create_hadamard_gate(0)
gates_created.append(("Hadamard", h_gate))
x_gate = core.create_pauli_x_gate(0)
gates_created.append(("Pauli-X", x_gate))
y_gate = core.create_pauli_y_gate(0)
gates_created.append(("Pauli-Y", y_gate))
z_gate = core.create_pauli_z_gate(0)
gates_created.append(("Pauli-Z", z_gate))
try:
import numpy as np
rx_gate = core.create_rotation_x_gate(0, np.pi/2)
gates_created.append(("RX(π/2)", rx_gate))
ry_gate = core.create_rotation_y_gate(0, np.pi/4)
gates_created.append(("RY(π/4)", ry_gate))
rz_gate = core.create_rotation_z_gate(0, np.pi/3)
gates_created.append(("RZ(π/3)", rz_gate))
except Exception as e:
print(f"⚠️ Rotation gates: {e}")
cnot_gate = core.create_cnot_gate(0, 1)
gates_created.append(("CNOT", cnot_gate))
try:
s_gate = core.create_s_gate(0)
gates_created.append(("S", s_gate))
t_gate = core.create_t_gate(0)
gates_created.append(("T", t_gate))
i_gate = core.create_identity_gate(0)
gates_created.append(("Identity", i_gate))
except Exception as e:
print(f"⚠️ Additional gates: {e}")
print(f"✅ Created {len(gates_created)} different quantum gates:")
for name, gate in gates_created:
print(f" {name}: {gate}")
try:
circuit = core.VariationalCircuit(4)
print(f"✅ Created variational circuit with {circuit.num_qubits} qubits")
print(f" Initial parameters: {circuit.num_parameters}")
circuit.add_rotation_layer("x")
circuit.add_entangling_layer()
circuit.add_rotation_layer("y")
print("✅ Added rotation and entangling layers to circuit")
except Exception as e:
print(f"⚠️ Variational circuit: {e}")
try:
import numpy as np
hadamard_matrix = np.array([
[1/np.sqrt(2), 1/np.sqrt(2)],
[1/np.sqrt(2), -1/np.sqrt(2)]
], dtype=complex)
decomp = core.decompose_single_qubit(hadamard_matrix)
print(f"✅ Single-qubit decomposition successful:")
print(f" θ₁ = {decomp.theta1:.6f}")
print(f" φ = {decomp.phi:.6f}")
print(f" θ₂ = {decomp.theta2:.6f}")
print(f" Global phase = {decomp.global_phase:.6f}")
cnot_matrix = np.array([
[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 0, 1],
[0, 0, 1, 0]
], dtype=complex)
cartan_decomp = core.decompose_two_qubit_cartan(cnot_matrix)
print(f"✅ Two-qubit Cartan decomposition successful:")
print(f" XX coefficient: {cartan_decomp.xx_coefficient:.6f}")
print(f" YY coefficient: {cartan_decomp.yy_coefficient:.6f}")
print(f" ZZ coefficient: {cartan_decomp.zz_coefficient:.6f}")
print(f" CNOT count: {cartan_decomp.cnot_count}")
except Exception as e:
print(f"⚠️ Decomposition: {e}")
try:
sensor_network = core.QuantumSensorNetwork(12345)
print(f"✅ Created quantum sensor network: {sensor_network}")
print(f" Network ID: {sensor_network.network_id}")
print(f" Initial sensors: {sensor_network.num_sensors()}")
sensor1_id = sensor_network.add_sensor("magnetometer", 37.7749, -122.4194)
sensor2_id = sensor_network.add_sensor("gravimeter", 40.7128, -74.0060)
print(f" Added sensors: {sensor1_id}, {sensor2_id}")
print(f" Total sensors: {sensor_network.num_sensors()}")
print(f" Quantum advantage: {sensor_network.get_sensor_advantage():.2f}")
except Exception as e:
print(f"⚠️ Quantum sensor network: {e}")
try:
internet = core.QuantumInternet()
print(f"✅ Created quantum internet: {internet}")
print(f" Coverage: {internet.get_coverage_percentage():.1f}%")
print(f" Nodes: {internet.get_node_count()}")
node1_id = internet.add_quantum_node(37.7749, -122.4194, "datacenter")
node2_id = internet.add_quantum_node(40.7128, -74.0060, "repeater")
print(f" Added nodes: {node1_id}, {node2_id}")
print(f" Updated coverage: {internet.get_coverage_percentage():.1f}%")
except Exception as e:
print(f"⚠️ Quantum internet: {e}")
try:
viz = core.QuantumCircuitVisualizer(3, "Test Circuit")
print(f"✅ Created circuit visualizer: {viz}")
viz.add_gate("H", [0], None, 0.99)
viz.add_gate("CNOT", [0, 1], None, 0.95)
viz.add_gate("RY", [2], [1.57], 0.98)
stats = viz.get_statistics()
print(f" Circuit statistics: {stats}")
except Exception as e:
print(f"⚠️ Visualization: {e}")
try:
zeros_array = core.numrs2_zeros([2, 2])
ones_array = core.numrs2_ones([2, 2])
print(f"✅ NumRS2 arrays created: {zeros_array.shape}, {ones_array.shape}")
sum_array = zeros_array.add(ones_array)
print(f" Array sum successful: {sum_array.shape}")
product = ones_array.matmul(ones_array)
print(f" Matrix multiplication successful: {product.shape}")
except Exception as e:
print(f"⚠️ NumRS2 integration: {e}")
try:
config = core.MonitoringConfig(monitoring_interval_secs=1.0, data_retention_hours=24.0)
print(f"✅ Created monitoring config: {config}")
monitor = core.RealtimeMonitor(config)
print(f"✅ Created real-time monitor: {monitor}")
except Exception as e:
print(f"⚠️ Real-time monitoring: {e}")
core_attributes = [attr for attr in dir(core) if not attr.startswith('_')]
print(f"\n✅ Available quantrs2.core attributes ({len(core_attributes)}):")
for i, attr in enumerate(sorted(core_attributes)):
if i % 4 == 0:
print(" ", end="")
print(f"{attr:<25}", end="")
if (i + 1) % 4 == 0:
print()
if len(core_attributes) % 4 != 0:
print()
print("\n" + "=" * 60)
print("🎉 QuantRS2 Core Integration Test PASSED!")
print("✅ Users can now use: from quantrs2.core import QubitId, create_hadamard_gate, ...")
print("✅ Users can also use: import quantrs2.core as core")
return True
except Exception as e:
print(f"\n❌ Integration test failed: {e}")
import traceback
traceback.print_exc()
return False
if __name__ == "__main__":
success = test_quantrs2_core_integration()
sys.exit(0 if success else 1)