赛前稳定版
Some checks failed
Build wheels / build (ubuntu-latest, 3.11) (push) Has been cancelled
Build wheels / build (ubuntu-latest, 3.12) (push) Has been cancelled
Build wheels / build (ubuntu-latest, 3.13) (push) Has been cancelled
Tests / check (push) Has been cancelled
Tests / build (ubuntu-latest, 3.11) (push) Has been cancelled
Tests / build (ubuntu-latest, 3.12) (push) Has been cancelled
Tests / build (ubuntu-latest, 3.13) (push) Has been cancelled

This commit is contained in:
2026-05-15 09:32:26 +08:00
parent 72f95599bb
commit 915c24dc7b
40 changed files with 5542 additions and 224 deletions

View File

@@ -5,7 +5,10 @@ from qibo import Circuit, gates, hamiltonians
from qibo.symbols import X, Z
from qibotn.backends.cpu import CpuTensorNet
from qibotn.benchmark_cases import build_circuit as build_benchmark_circuit
from qibotn.benchmark_cases import (
build_circuit as build_benchmark_circuit,
exact_pauli_sum,
)
def build_circuit(nqubits=6):
@@ -109,6 +112,59 @@ def test_cpu_mps_sampling_uses_nshots():
assert set(result.frequencies()) <= {"0000", "1111"}
def test_cpu_mps_mpo_expectation_matches_statevector():
circuit = build_circuit(nqubits=4)
x = np.array([[0, 1], [1, 0]], dtype=complex)
z = np.array([[1, 0], [0, -1]], dtype=complex)
i2 = np.eye(2, dtype=complex)
mpo = [
x.reshape(1, 2, 2, 1),
z.reshape(1, 2, 2, 1),
i2.reshape(1, 2, 2, 1),
i2.reshape(1, 2, 2, 1),
]
exact = exact_pauli_sum(circuit, [(1.0, (("X", 0), ("Z", 1)))], 4)
backend = CpuTensorNet(
{
"MPI_enabled": False,
"MPS_enabled": True,
"NCCL_enabled": False,
"expectation_enabled": {"mpo_tensors": mpo},
"max_bond_dimension": 64,
"tensor_module": "torch",
"torch_threads": 1,
}
)
value = backend.execute_circuit(circuit)[0]
assert math.isclose(value, exact, abs_tol=1e-12)
def test_cpu_mps_dense_observable_dict_matches_known_value():
circuit = Circuit(2)
circuit.add(gates.H(0))
circuit.add(gates.CNOT(0, 1))
bell = np.zeros((4, 4), dtype=complex)
bell[0, 0] = bell[0, 3] = bell[3, 0] = bell[3, 3] = 0.5
backend = CpuTensorNet(
{
"MPI_enabled": False,
"MPS_enabled": True,
"NCCL_enabled": False,
"expectation_enabled": {"matrix": bell, "qubits": [0, 1]},
"max_bond_dimension": 16,
"tensor_module": "torch",
"torch_threads": 1,
}
)
value = backend.execute_circuit(circuit)[0]
assert math.isclose(value, 1.0, abs_tol=1e-12)
def test_cpu_generic_tn_long_pauli_string_matches_statevector():
circuit = build_benchmark_circuit("rxx_rzz", 10, 2, 42)
observable = {"pauli_string_pattern": "XZ"}