Skip to content

[Bug]: TensorNetworkBackend ignores the order of output nodes #323

@thierry-martinez

Description

@thierry-martinez

TensorNetworkBackend ignores the order of output nodes. The current implementation of finalize is:

    def finalize(self, output_nodes) -> None:
        """Do nothing."""

This means that the resulting tensor network returns a state vector in an unexpected order, as demonstrated by the following test, which currently fails.

    @pytest.mark.parametrize(("nqubits", "jumps"), itertools.product(range(2, 6), range(1, 6)))
    def test_to_statevector_order(self, fx_bg: PCG64, nqubits: int, jumps: int, fx_rng: Generator) -> None:
        rng = Generator(fx_bg.jumped(jumps))
        circuit = rand_circuit(nqubits, 3, rng)
        pattern = circuit.transpile().pattern
        pattern.standardize()
        pattern.shift_signals()
        statevec_ref = circuit.simulate_statevector().statevec
        pattern.reorder_output_nodes(reversed(pattern.output_nodes))

        tn = pattern.simulate_pattern("tensornetwork", rng=fx_rng)
        statevec_tn = tn.to_statevector()

        inner_product = np.inner(statevec_tn, statevec_ref.flatten().conjugate())
        assert abs(inner_product) == pytest.approx(1)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions