Skip to content

Conversation

@mateuszkupper
Copy link
Collaborator

@mateuszkupper mateuszkupper commented Aug 6, 2025

Instead of specifying a truncation method returning a tensor.Box for all optyx (core) diagrams, a generic truncation method is given for optyx.core.diagrams.Box which uses _truncation_specification which specifies the mapping between input basis vectors to output basis vectors. Only such _truncation_specification method needs to be implemented for all boxes inheriting from optyx.core.diagrams.Box.

Example for W (white triangle):

    def truncation_specification(
        self,
        inp: Tuple[int, ...],
        max_output_dims: Tuple[int, ...] = None
    ) -> Iterable[BasisTransition]:

        for config in occupation_numbers(inp[0], self.n_legs):
            if all(
                config[i] < max_output_dims[i] for i in range(self.n_legs)
            ):
                yield BasisTransition(
                    out=tuple(config),
                    amp=multinomial(config) ** 0.5
                )

Example for Add:

    def truncation_specification(
        self,
        inp: Tuple[int, ...],
        max_output_dims: Tuple[int, ...] = None
    ) -> Iterable[BasisTransition]:
        n = sum(inp)
        if n < int(max_output_dims[0]):
            yield BasisTransition(out=(n,), amp=1.0)

For each input vector (a tuple with a photon number config), we return an output basis vector (photon number configs) with corresponding value which then can be used to construct an array for a tensor. This is done through a named tuple BasisTransition with params out and amp.

Not all current boxes implement this method. For some, truncation is overridden directly (especially when we can give a more efficient implementation) - like for ZBox.

This PR simplifies the truncation logic by moving the common tensor/array building code into a shared method.

@mateuszkupper mateuszkupper marked this pull request as ready for review August 26, 2025 09:14
@giodefelice giodefelice merged commit 551bcb3 into main Aug 28, 2025
3 checks passed
mateuszkupper added a commit that referenced this pull request Aug 28, 2025
Adds a class `BasisTransition` for specifying output vectors of boxes with amplitudes, and a method `truncation_specification` for specifying the underlying truncated array of custom boxes.
mateuszkupper added a commit that referenced this pull request Aug 28, 2025
Adds a class `BasisTransition` for specifying output vectors of boxes with amplitudes, and a method `truncation_specification` for specifying the underlying truncated array of custom boxes.
mateuszkupper added a commit that referenced this pull request Aug 28, 2025
Adds a class `BasisTransition` for specifying output vectors of boxes with amplitudes, and a method `truncation_specification` for specifying the underlying truncated array of custom boxes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants