赛前稳定版
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

@@ -2,10 +2,18 @@ import math
import numpy as np
from qibo import Circuit, gates, hamiltonians
from qibo.symbols import X, Y, Z
from qibo.symbols import Symbol, X, Y, Z
from qibotn.backends.vidal import VidalBackend, _can_route_non_adjacent, _unsupported_reason
from qibotn.benchmark_cases import exact_pauli_sum
from qibotn.backends.vidal import (
VidalBackend,
_can_route_non_adjacent,
_unsupported_reason,
_operator_terms_to_mpo,
_symbolic_hamiltonian_to_operator_terms,
)
from qibotn.backends.vidal_tebd import (
VidalTEBDExecutor,
_route_non_adjacent_gates,
_gate_sites,
)
@@ -37,6 +45,25 @@ def test_vidal_backend_expectation_matches_statevector():
np.testing.assert_allclose(value, exact, atol=1e-12)
def test_vidal_backend_accepts_unlimited_bond_and_no_cutoff():
circuit = build_local_circuit(nqubits=6, nlayers=2)
observable = hamiltonians.SymbolicHamiltonian(
form=0.5 * X(0) * Z(1) - 0.7 * Z(5)
)
exact = observable.expectation_from_state(circuit().state(numpy=True))
backend = VidalBackend()
backend.configure_tn_simulation(
max_bond_dimension=None,
cut_ratio=None,
tensor_module="torch",
fallback=False,
)
value = backend.expectation(circuit, observable, preprocess=False)
np.testing.assert_allclose(value, exact, atol=1e-12)
def test_vidal_backend_fallback_for_non_adjacent_gate():
"""compile_circuit=False (default) → falls back to qmatchatea for non-adjacent."""
circuit = Circuit(4)
@@ -128,6 +155,208 @@ def test_routing_non_adjacent_cnot():
np.testing.assert_allclose(value, exact, atol=1e-12)
def test_routing_preserves_reversed_non_adjacent_gate_order():
circuit = Circuit(6)
circuit.add(gates.X(5))
circuit.add(gates.H(0))
circuit.add(gates.CNOT(5, 0))
observable = hamiltonians.SymbolicHamiltonian(form=X(0) + Z(5) + Z(0) * Z(5))
exact = observable.expectation_from_state(circuit().state(numpy=True))
backend = VidalBackend()
backend.configure_tn_simulation(
max_bond_dimension=64,
tensor_module="torch",
compile_circuit=True,
fallback=False,
)
value = backend.expectation(circuit, observable, preprocess=False)
np.testing.assert_allclose(value, exact, atol=1e-12)
def test_vidal_backend_preprocesses_non_adjacent_circuit():
circuit = Circuit(4)
circuit.add(gates.H(0))
circuit.add(gates.CNOT(0, 3))
observable = hamiltonians.SymbolicHamiltonian(form=Z(0) * Z(3))
exact = observable.expectation_from_state(circuit().state(numpy=True))
backend = VidalBackend()
backend.configure_tn_simulation(
max_bond_dimension=64,
tensor_module="torch",
compile_circuit=True,
fallback=False,
)
value = backend.expectation(circuit, observable, preprocess=True)
np.testing.assert_allclose(value, exact, atol=1e-12)
def test_vidal_backend_preprocesses_toffoli_locally():
circuit = Circuit(4)
circuit.add(gates.H(0))
circuit.add(gates.H(1))
circuit.add(gates.TOFFOLI(0, 1, 3))
observable = hamiltonians.SymbolicHamiltonian(form=Z(0) * Z(3))
exact = observable.expectation_from_state(circuit().state(numpy=True))
backend = VidalBackend()
backend.configure_tn_simulation(
max_bond_dimension=128,
tensor_module="torch",
compile_circuit=True,
fallback=False,
)
value = backend.expectation(circuit, observable, preprocess=True)
np.testing.assert_allclose(value, exact, atol=1e-12)
def test_vidal_expectation_preserves_complex_coefficients():
circuit = Circuit(1)
observable = hamiltonians.SymbolicHamiltonian(form=(1.0 + 2.0j) * Z(0))
backend = VidalBackend()
backend.configure_tn_simulation(
max_bond_dimension=8,
tensor_module="torch",
fallback=False,
)
value = backend.expectation(circuit, observable, preprocess=False)
np.testing.assert_allclose(value, 1.0 + 2.0j, atol=1e-12)
def test_vidal_expectation_supports_custom_local_symbols():
circuit = build_local_circuit(nqubits=4, nlayers=2)
a0 = Symbol(0, np.array([[0.2, 1.0], [1.0, -0.3]], dtype=complex), name="A")
b2 = Symbol(2, np.array([[0.7, -0.4j], [0.4j, 0.1]], dtype=complex), name="B")
a3 = Symbol(3, np.array([[0.5, 0.2], [0.2, -0.8]], dtype=complex), name="A")
observable = hamiltonians.SymbolicHamiltonian(form=0.7 * a0 * b2 - 0.4 * a3)
exact = observable.expectation_from_state(circuit().state(numpy=True))
backend = VidalBackend()
backend.configure_tn_simulation(
max_bond_dimension=64,
tensor_module="torch",
fallback=False,
)
value = backend.expectation(circuit, observable, preprocess=False)
np.testing.assert_allclose(value, exact, atol=1e-12)
def test_vidal_executor_mpo_expectation_matches_pauli_sum():
circuit = build_local_circuit(nqubits=4, nlayers=2)
executor = VidalTEBDExecutor(
nqubits=circuit.nqubits,
max_bond=64,
tensor_module="torch",
)
executor.run_circuit(circuit)
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),
]
mpo_value = executor.expectation_mpo(mpo)
pauli_value = executor.expectation_pauli_sum([(1.0, (("X", 0), ("Z", 1)))])
np.testing.assert_allclose(mpo_value, pauli_value, atol=1e-12)
def test_vidal_backend_accepts_mpo_observable_dict():
circuit = build_local_circuit(nqubits=4, nlayers=2)
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 = VidalBackend()
backend.configure_tn_simulation(
max_bond_dimension=64,
tensor_module="torch",
fallback=False,
)
value = backend.expectation(circuit, {"mpo_tensors": mpo}, preprocess=False)
np.testing.assert_allclose(value, exact, atol=1e-12)
def test_vidal_symbolic_hamiltonian_auto_mpo_matches_operator_sum():
circuit = build_local_circuit(nqubits=5, nlayers=2)
observable = hamiltonians.SymbolicHamiltonian(
form=0.3 * X(0) * Z(1) - 0.2j * Y(2) + 0.7 * Z(3) * X(4)
)
executor = VidalTEBDExecutor(
nqubits=circuit.nqubits,
max_bond=64,
tensor_module="torch",
)
executor.run_circuit(circuit)
terms = _symbolic_hamiltonian_to_operator_terms(observable)
term_value = executor.expectation_operator_sum(terms)
mpo_value = executor.expectation_mpo(_operator_terms_to_mpo(terms, circuit.nqubits))
np.testing.assert_allclose(mpo_value, term_value, atol=1e-12)
def test_vidal_backend_accepts_dense_two_qubit_observable():
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
observable = {"matrix": bell, "qubits": [0, 1]}
backend = VidalBackend()
backend.configure_tn_simulation(
max_bond_dimension=16,
tensor_module="torch",
fallback=False,
)
value = backend.expectation(circuit, observable, preprocess=False)
np.testing.assert_allclose(value, 1.0, atol=1e-12)
def test_vidal_backend_dense_observable_preserves_complex_value():
circuit = Circuit(2)
circuit.add(gates.H(0))
circuit.add(gates.H(1))
op = np.zeros((4, 4), dtype=complex)
op[0, 3] = 1.0
observable = {"coefficient": 1.0j, "matrix": op, "qubits": [0, 1]}
backend = VidalBackend()
backend.configure_tn_simulation(
max_bond_dimension=16,
tensor_module="torch",
fallback=False,
)
value = backend.expectation(circuit, observable, preprocess=False)
np.testing.assert_allclose(value, 0.25j, atol=1e-12)
def test_truncation_error_no_truncation():
"""With large bond, truncation error should be essentially zero."""
circuit = build_local_circuit(nqubits=6, nlayers=2)
@@ -141,6 +370,10 @@ def test_truncation_error_no_truncation():
assert backend.last_truncation_error < 1e-14, (
f"Expected near-zero truncation error, got {backend.last_truncation_error}"
)
assert backend.last_max_truncation_error < 1e-14, (
"Expected near-zero max truncation error, got "
f"{backend.last_max_truncation_error}"
)
def test_vidal_backend_matches_statevector_multiterm():