def test_uccsd_singlet_builds(self):
        """Test specific builds of the UCCSD singlet operator"""
        # Build 1
        n_orbitals = 4
        n_electrons = 2
        n_params = uccsd_singlet_paramsize(n_orbitals, n_electrons)
        self.assertEqual(n_params, 2)

        initial_amplitudes = [1., 2.]

        generator = uccsd_singlet_generator(initial_amplitudes,
                                            n_orbitals,
                                            n_electrons)

        test_generator = (FermionOperator("2^ 0", 1.) +
                          FermionOperator("0^ 2", -1.) +
                          FermionOperator("3^ 1", 1.) +
                          FermionOperator("1^ 3", -1.) +
                          FermionOperator("2^ 0 3^ 1", 4.) +
                          FermionOperator("1^ 3 0^ 2", -4.))

        self.assertEqual(normal_ordered(test_generator),
                         normal_ordered(generator))

        # Build 2
        n_orbitals = 6
        n_electrons = 2

        n_params = uccsd_singlet_paramsize(n_orbitals, n_electrons)
        self.assertEqual(n_params, 5)

        initial_amplitudes = numpy.arange(1, n_params + 1, dtype=float)
        generator = uccsd_singlet_generator(initial_amplitudes,
                                            n_orbitals,
                                            n_electrons)

        test_generator = (FermionOperator("2^ 0", 1.) +
                          FermionOperator("0^ 2", -1) +
                          FermionOperator("3^ 1", 1.) +
                          FermionOperator("1^ 3", -1.) +
                          FermionOperator("4^ 0", 2.) +
                          FermionOperator("0^ 4", -2) +
                          FermionOperator("5^ 1", 2.) +
                          FermionOperator("1^ 5", -2.) +
                          FermionOperator("2^ 0 3^ 1", 6.) +
                          FermionOperator("1^ 3 0^ 2", -6.) +
                          FermionOperator("4^ 0 5^ 1", 8.) +
                          FermionOperator("1^ 5 0^ 4", -8.) +
                          FermionOperator("2^ 0 5^ 1", 5.) +
                          FermionOperator("1^ 5 0^ 2", -5.) +
                          FermionOperator("4^ 0 3^ 1", 5.) +
                          FermionOperator("1^ 3 0^ 4", -5.) +
                          FermionOperator("2^ 0 4^ 0", 5.) +
                          FermionOperator("0^ 4 0^ 2", -5.) +
                          FermionOperator("3^ 1 5^ 1", 5.) +
                          FermionOperator("1^ 5 1^ 3", -5.))

        self.assertEqual(normal_ordered(test_generator),
                         normal_ordered(generator))
    def test_uccsd_singlet_anti_hermitian(self):
        """Test that the singlet version is anti-Hermitian"""
        test_orbitals = 8
        test_electrons = 4

        packed_amplitude_size = uccsd_singlet_paramsize(
            test_orbitals, test_electrons)

        packed_amplitudes = randn(int(packed_amplitude_size))

        generator = uccsd_singlet_generator(packed_amplitudes, test_orbitals,
                                            test_electrons)

        conj_generator = hermitian_conjugated(generator)

        self.assertEqual(generator, -1. * conj_generator)
    def test_uccsd_singlet_symmetries(self):
        """Test that the singlet generator has the correct symmetries."""
        test_orbitals = 8
        test_electrons = 4

        packed_amplitude_size = uccsd_singlet_paramsize(
            test_orbitals, test_electrons)
        packed_amplitudes = randn(int(packed_amplitude_size))
        generator = uccsd_singlet_generator(packed_amplitudes, test_orbitals,
                                            test_electrons)

        # Construct symmetry operators
        sz = sz_operator(test_orbitals)
        s_squared = s_squared_operator(test_orbitals)

        # Check the symmetries
        comm_sz = normal_ordered(commutator(generator, sz))
        comm_s_squared = normal_ordered(commutator(generator, s_squared))
        zero = FermionOperator()

        self.assertEqual(comm_sz, zero)
        self.assertEqual(comm_s_squared, zero)
 def test_value_error_for_odd_n_qubits(self):
     # Pass odd n_qubits to singlet generators
     with self.assertRaises(ValueError):
         _ = uccsd_singlet_paramsize(3, 4)
 def test_exceptions(self):
     # Pass odd n_qubits to singlet generators
     with self.assertRaises(ValueError):
         _ = uccsd_singlet_paramsize(3, 4)
     with self.assertRaises(ValueError):
         _ = uccsd_singlet_generator([1.], 3, 4)