65 lines
2.4 KiB
Python
65 lines
2.4 KiB
Python
# example_quantum.py
|
|
|
|
import numpy as np
|
|
from artifact import Artifact, bits, tensor, materialize_artifact, dag_node_count, dag_depth, ArtifactCache
|
|
from sid_hashers import SHA256SIDHash
|
|
from hashers import SHA256Hash
|
|
|
|
# ---------------------------------------------------------------------
|
|
# Hashers
|
|
# ---------------------------------------------------------------------
|
|
sid_hasher = SHA256SIDHash()
|
|
content_hasher = SHA256Hash()
|
|
|
|
# ---------------------------------------------------------------------
|
|
# Step 1: Create 8 quantum leaves (1 qubit each)
|
|
# We'll make simple |0> + |1> superposition for each qubit
|
|
# ---------------------------------------------------------------------
|
|
quantum_leaves = []
|
|
for i in range(8):
|
|
indices = np.array([0, 1], dtype=np.int64)
|
|
values = np.array([1+0j, 1+0j], dtype=np.complex128)
|
|
leaf = Artifact(
|
|
op="leaf.bits",
|
|
params={"_materialized": (indices, values)}, # mandatory for materialization
|
|
children=[],
|
|
sid=f"qubit_{i}_superposition",
|
|
materializer=materialize_artifact,
|
|
content_hasher=content_hasher,
|
|
)
|
|
quantum_leaves.append(leaf)
|
|
|
|
# ---------------------------------------------------------------------
|
|
# Step 2: Tensor all 8 qubits together lazily
|
|
# ---------------------------------------------------------------------
|
|
# Tensor pairs recursively
|
|
def tensor_all(artifacts, sid_hasher):
|
|
if len(artifacts) == 1:
|
|
return artifacts[0]
|
|
mid = len(artifacts) // 2
|
|
left = tensor_all(artifacts[:mid], sid_hasher)
|
|
right = tensor_all(artifacts[mid:], sid_hasher)
|
|
return tensor(left, right, sid_hasher=sid_hasher)
|
|
|
|
quantum_8q = tensor_all(quantum_leaves, sid_hasher=sid_hasher)
|
|
|
|
print("8-qubit quantum tensor SID (lazy):")
|
|
print(quantum_8q.sid)
|
|
|
|
print("CID materialized yet?", quantum_8q.is_materialized)
|
|
|
|
# ---------------------------------------------------------------------
|
|
# Step 3: Materialize CID on demand
|
|
# ---------------------------------------------------------------------
|
|
cid_8q = quantum_8q.cid
|
|
print("\nAfter materialization:")
|
|
print("8-qubit quantum tensor CID:", cid_8q)
|
|
|
|
# ---------------------------------------------------------------------
|
|
# Step 4: DAG metrics
|
|
# ---------------------------------------------------------------------
|
|
print("\nDerivation DAG metrics:")
|
|
print("Total nodes:", dag_node_count(quantum_8q))
|
|
print("DAG depth:", dag_depth(quantum_8q))
|
|
|