Skip to content

Utils

check_circuit

check_circuit(
    squin_method: Method[[], None],
    other_squin_method: Method[[], None],
    atol: float = 1e-05,
    rtol: float = 1e-08,
) -> bool

Check if two squin methods are equivalent.

Parameters:

Name Type Description Default
squin_method Method[[], None]

The first squin method. This method should not take any arguments and return None.

required
other_squin_method Method[[], None]

The second squin method. This method should not take any arguments and return None.

required
atol float

Absolute tolerance for state vector comparison. Defaults to 1.0e-5.

1e-05
rtol float

Relative tolerance for state vector comparison. Defaults to 1

1e-08

Returns:

Name Type Description
bool bool

True if the methods are equivalent, False otherwise.

Note

The methods should not perform any measurements. Otherwise, the state vectors may not be comparable.

Source code in .venv/lib/python3.12/site-packages/bloqade/lanes/utils.py
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
def check_circuit(
    squin_method: ir.Method[[], None],
    other_squin_method: ir.Method[[], None],
    atol: float = 1.0e-5,
    rtol: float = 1.0e-8,
) -> bool:
    """Check if two squin methods are equivalent.

    Args:
        squin_method (ir.Method[[], None]): The first squin method. This method should not take
            any arguments and return None.
        other_squin_method (ir.Method[[], None]): The second squin method. This method should not take
            any arguments and return None.
        atol (float, optional): Absolute tolerance for state vector comparison. Defaults to 1.0e-5.
        rtol (float, optional): Relative tolerance for state vector comparison. Defaults to 1

    Returns:
        bool: True if the methods are equivalent, False otherwise.

    Note:
        The methods should not perform any measurements. Otherwise, the state vectors may not be comparable.
    """
    from bloqade.pyqrack.device import StackMemorySimulator

    simulator = StackMemorySimulator()
    state_vector = np.asarray(simulator.state_vector(squin_method))
    other_state_vector = np.asarray(simulator.state_vector(other_squin_method))

    i = np.argmax(np.abs(state_vector))
    j = np.argmax(np.abs(other_state_vector))
    state_vector *= np.exp(-1j * np.angle(state_vector[i]))
    other_state_vector *= np.exp(-1j * np.angle(other_state_vector[j]))

    if state_vector.shape != other_state_vector.shape:
        return False

    return np.allclose(state_vector, other_state_vector, atol=atol, rtol=rtol)

no_none_elements

no_none_elements(
    xs: Sequence[T | None],
) -> TypeGuard[Sequence[T]]

Check that there are no None elements in the sequence.

Parameters:

Name Type Description Default
xs Sequence[T | None]

A sequence that may contain None elements.

required

Returns:

Type Description
TypeGuard[Sequence[T]]

A TypeGuard indicating that all elements are not None.

Source code in .venv/lib/python3.12/site-packages/bloqade/lanes/utils.py
 9
10
11
12
13
14
15
16
17
18
19
def no_none_elements(xs: Sequence[T | None]) -> TypeGuard[Sequence[T]]:
    """Check that there are no None elements in the sequence.

    Args:
        xs: A sequence that may contain None elements.

    Returns:
        A TypeGuard indicating that all elements are not None.

    """
    return all(x is not None for x in xs)

no_none_elements_tuple

no_none_elements_tuple(
    xs: tuple[T | None, ...],
) -> TypeGuard[tuple[T, ...]]

Check that there are no None elements in the tuple.

Parameters:

Name Type Description Default
xs tuple[T | None, ...]

A tuple that may contain None elements.

required

Returns:

Type Description
TypeGuard[tuple[T, ...]]

A TypeGuard indicating that all elements are not None.

Source code in .venv/lib/python3.12/site-packages/bloqade/lanes/utils.py
22
23
24
25
26
27
28
29
30
31
32
def no_none_elements_tuple(xs: tuple[T | None, ...]) -> TypeGuard[tuple[T, ...]]:
    """Check that there are no None elements in the tuple.

    Args:
        xs: A tuple that may contain None elements.

    Returns:
        A TypeGuard indicating that all elements are not None.

    """
    return all(x is not None for x in xs)