def test_ffft_2d_4x4_logical_state(self):
        system_size = 4
        grid = Grid(dimensions=2, length=system_size, scale=1.0)

        fermion_operator = FermionOperator('12^ 11^ 4^ 3^')
        ffft_result = operator_2d_fft_with_reordering(
            fermion_operator, system_size)

        eng = MainEngine()
        reg = eng.allocate_qureg(system_size ** 2)
        X | reg[3]
        X | reg[4]
        X | reg[11]
        X | reg[12]
        ffft_2d(eng, reg, system_size)
        eng.flush()

        engine_wvfn = ordered_wavefunction(eng)
        operator_wvfn = numpy.zeros(2 ** (system_size ** 2), dtype=complex)
        for term in ffft_result.terms:
            i = sum(2 ** site[0] for site in term)
            operator_wvfn[i] = ffft_result.terms[term]

        All(Measure) | reg

        self.assertTrue(numpy.allclose(engine_wvfn, operator_wvfn))
    def test_ffft_2d_4x4_equal_expectation_values(self):
        system_size = 4
        n_qubits = 16
        grid = Grid(dimensions=2, length=system_size, scale=1.0)
        dual_basis = jellium_model(grid, spinless=True, plane_wave=False)
        ffft_result = operator_2d_fft_with_reordering(dual_basis, system_size)

        jw_dual_basis = jordan_wigner(dual_basis)
        jw_plane_wave = jordan_wigner(ffft_result)

        # Do plane wave and dual basis calculations together.
        pw_engine = MainEngine()
        pw_wavefunction = pw_engine.allocate_qureg(system_size ** 2)
        db_engine = MainEngine()
        db_wavefunction = db_engine.allocate_qureg(system_size ** 2)

        pw_engine.flush()
        db_engine.flush()

        # Choose random state.
        state = numpy.zeros(2 ** n_qubits, dtype=complex)
        for i in range(len(state)):
            state[i] = (random.random() *
                        numpy.exp(1j * 2 * numpy.pi * random.random()))
        state /= numpy.linalg.norm(state)

        # Put randomly chosen state in the registers.
        pw_engine.backend.set_wavefunction(state, pw_wavefunction)
        db_engine.backend.set_wavefunction(state, db_wavefunction)

        # Apply the FFFT to the dual basis wave function.
        ffft_2d(db_engine, db_wavefunction, system_size)

        # Flush the engine and compute expectation values and eigenvalues.
        pw_engine.flush()
        db_engine.flush()

        plane_wave_expectation_value = (
            pw_engine.backend.get_expectation_value(
                jw_dual_basis, pw_wavefunction))
        dual_basis_expectation_value = (
            db_engine.backend.get_expectation_value(
                jw_plane_wave, db_wavefunction))

        All(Measure) | pw_wavefunction
        All(Measure) | db_wavefunction

        self.assertAlmostEqual(plane_wave_expectation_value,
                               dual_basis_expectation_value)