File size: 1,978 Bytes
eef9104
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# *******************************************************************************
# Copyright (c) 2026 Ahmad Ali Parr and others.
#
# This program and the accompanying materials are made available under the
# terms of the CPSC Eclipse Strict Copyleft License, Version 1.0, which is
# available in LICENSES/CPSC-ESCL-1.0.txt, or, at your option, under the
# GNU Affero General Public License, Version 3 only, which is available in
# LICENSES/AGPL-3.0.txt. Both options are strict copyleft. There is no
# classpath exception and no permissive relicensing.
#
# SPDX-License-Identifier: LicenseRef-CPSC-ESCL-1.0 OR AGPL-3.0-only
# *******************************************************************************

import numpy as np
import pytest

from cpsc.channel import evolve_in_sector, leakage
from cpsc.hamiltonian import magnetization_of, parity_of, transverse_ising, x4_identity_shift


def test_x4_is_proportional_to_identity():
    # The draft quartic does not open a new scattering channel.
    assert x4_identity_shift(5, 1.2) == pytest.approx(5 * 1.2 / 24.0)


def test_sector_evolution_has_zero_parity_leakage():
    n = 4
    initial = 0b0101
    psi = evolve_in_sector(n, initial, time=0.7, j=1.0, lam=0.4)
    assert leakage(psi, n, parity_of(initial, n)) < 1e-10
    assert abs(np.vdot(psi, psi) - 1) < 1e-10


def test_magnetization_projection_refused_when_field_is_on():
    with pytest.raises(ValueError):
        evolve_in_sector(3, 0b001, time=0.2, lam=0.3, conserve_magnetization=True)


def test_ising_limit_stays_in_weight_sector():
    n = 4
    initial = 0b0011
    psi = evolve_in_sector(
        n, initial, time=0.5, j=1.0, lam=0.0, conserve_magnetization=True
    )
    weight = magnetization_of(initial, n)
    for basis, amp in enumerate(psi):
        if abs(amp) > 1e-10:
            assert magnetization_of(basis, n) == weight


def test_hamiltonian_is_hermitian():
    h = transverse_ising(3, j=0.8, lam=0.3)
    assert np.allclose(h, h.conj().T)