def test_bogoliubov_transform_compose_general(self, n_orbitals):
        """Test Bogoliubov transform composition, general."""
        hamiltonian1 = random_quadratic_hamiltonian(n_orbitals,
                                                    num_conserving=False,
                                                    seed=6990)
        hamiltonian2 = random_quadratic_hamiltonian(n_orbitals,
                                                    num_conserving=False,
                                                    seed=1447)
        transformation_matrix1, _, _ = hamiltonian1.diagonalizing_bogoliubov_transform(
        )
        transformation_matrix2, _, _ = hamiltonian2.diagonalizing_bogoliubov_transform(
        )
        composed_transformation_matrix = (
            _expand_transformation_matrix(transformation_matrix1)
            @ _expand_transformation_matrix(transformation_matrix2)
        )[:n_orbitals]

        bog_circuit_1 = BogoliubovTransform(transformation_matrix1)
        bog_circuit_2 = BogoliubovTransform(transformation_matrix2)
        bog_circuit_composed = BogoliubovTransform(
            composed_transformation_matrix)

        register = QuantumRegister(n_orbitals)
        circuit = QuantumCircuit(register)
        circuit.append(bog_circuit_1, register)
        circuit.append(bog_circuit_2, register)

        self.assertTrue(
            Operator(circuit).equiv(Operator(bog_circuit_composed), atol=1e-8))
 def test_fermionic_gaussian_state(self):
     """Test preparing fermionic Gaussian states."""
     n_orbitals = 5
     converter = QubitConverter(JordanWignerMapper())
     quad_ham = random_quadratic_hamiltonian(n_orbitals, seed=5957)
     (
         transformation_matrix,
         orbital_energies,
         transformed_constant,
     ) = quad_ham.diagonalizing_bogoliubov_transform()
     fermionic_op = quad_ham.to_fermionic_op()
     qubit_op = converter.convert(fermionic_op)
     matrix = qubit_op.to_matrix()
     occupied_orbitals_lists = [
         [],
         [0],
         [3],
         [0, 1],
         [2, 4],
         [1, 3, 4],
         range(n_orbitals),
     ]
     for occupied_orbitals in occupied_orbitals_lists:
         circuit = FermionicGaussianState(transformation_matrix,
                                          occupied_orbitals,
                                          qubit_converter=converter)
         final_state = np.array(Statevector(circuit))
         eig = np.sum(
             orbital_energies[occupied_orbitals]) + transformed_constant
         np.testing.assert_allclose(matrix @ final_state,
                                    eig * final_state,
                                    atol=1e-7)
 def test_no_side_effects(self):
     """Test that the routines don't mutate the input array."""
     n_orbitals = 5
     quad_ham = random_quadratic_hamiltonian(n_orbitals, seed=8353)
     transformation_matrix, _, _ = quad_ham.diagonalizing_bogoliubov_transform(
     )
     original = transformation_matrix.copy()
     _ = FermionicGaussianState(transformation_matrix,
                                occupied_orbitals=[2, 3])
     np.testing.assert_allclose(transformation_matrix, original, atol=1e-7)
 def test_no_side_effects(self):
     """Test that the routines don't mutate the input array."""
     n_orbitals = 5
     n_particles = 3
     quad_ham = random_quadratic_hamiltonian(n_orbitals,
                                             num_conserving=True,
                                             seed=8839)
     transformation_matrix, _, _ = quad_ham.diagonalizing_bogoliubov_transform(
     )
     original = transformation_matrix.copy()
     _ = SlaterDeterminant(transformation_matrix[:n_particles])
     np.testing.assert_allclose(transformation_matrix, original, atol=1e-7)
 def test_slater_determinant(self):
     """Test preparing Slater determinants."""
     n_orbitals = 5
     converter = QubitConverter(JordanWignerMapper())
     quad_ham = random_quadratic_hamiltonian(n_orbitals,
                                             num_conserving=True,
                                             seed=8839)
     (
         transformation_matrix,
         orbital_energies,
         transformed_constant,
     ) = quad_ham.diagonalizing_bogoliubov_transform()
     fermionic_op = quad_ham.to_fermionic_op()
     qubit_op = converter.convert(fermionic_op)
     matrix = qubit_op.to_matrix()
     for n_particles in range(n_orbitals + 1):
         circuit = SlaterDeterminant(transformation_matrix[:n_particles],
                                     qubit_converter=converter)
         final_state = np.array(Statevector(circuit))
         eig = np.sum(orbital_energies[:n_particles]) + transformed_constant
         np.testing.assert_allclose(matrix @ final_state,
                                    eig * final_state,
                                    atol=1e-7)
 def test_bogoliubov_transform(self, n_orbitals, num_conserving):
     """Test Bogoliubov transform."""
     converter = QubitConverter(JordanWignerMapper())
     hamiltonian = random_quadratic_hamiltonian(
         n_orbitals, num_conserving=num_conserving, seed=5740)
     (
         transformation_matrix,
         orbital_energies,
         transformed_constant,
     ) = hamiltonian.diagonalizing_bogoliubov_transform()
     matrix = converter.map(hamiltonian.to_fermionic_op()).to_matrix()
     bog_circuit = BogoliubovTransform(transformation_matrix,
                                       qubit_converter=converter)
     for initial_state in range(2**n_orbitals):
         state = Statevector.from_int(initial_state, dims=2**n_orbitals)
         final_state = np.array(state.evolve(bog_circuit))
         occupied_orbitals = [
             i for i in range(n_orbitals) if initial_state >> i & 1
         ]
         eig = np.sum(
             orbital_energies[occupied_orbitals]) + transformed_constant
         np.testing.assert_allclose(matrix @ final_state,
                                    eig * final_state,
                                    atol=1e-8)