def test_d2_spin_sz_rep(): n_density, rdm_generator, transform, molecule = system() assert np.isclose(molecule.fci_energy, -2.84383506834) density = AntiSymmOrbitalDensity(n_density, molecule.n_qubits) tpdm_aa, tpdm_bb, tpdm_ab, _ = density.construct_tpdm() opdm_a, opdm_b = density.construct_opdm() bas_aa, bas_ab = geminal_spin_basis(molecule.n_orbitals) tpdm_ab = Tensor(tpdm_ab, name='cckk_ab', basis=bas_ab) opdm_a = Tensor(opdm_a, name='ck_a') opdm_b = Tensor(opdm_b, name='ck_b') rdms = MultiTensor([opdm_a, opdm_b, tpdm_ab]) dim = int(np.sqrt(tpdm_ab.data.shape[0])) sz_rep_value = 0 for i in range(dim): sz_rep_value += 0.5 * (opdm_a.data[i, i] - opdm_b.data[i, i]) N = molecule.n_electrons M = 0 S = 0 db = DualBasis() db += s_representability_d2ab(dim, N, M, S) db += sz_representability(dim, M) rdms.dual_basis = db xvec = rdms.vectorize_tensors() A, _, b = rdms.synthesize_dual_basis() assert np.allclose(A.dot(xvec) - b, 0.0)
def test_d2_q2_mapping_hubbard(): n_density, rdm_generator = system_hubbard() density = AntiSymmOrbitalDensity(n_density, 8) tpdm_aa, tpdm_bb, tpdm_ab, _ = density.construct_tpdm() tqdm_aa, tqdm_bb, tqdm_ab, _ = density.construct_thdm() opdm_a, opdm_b = density.construct_opdm() bas_aa, bas_ab = geminal_spin_basis(4) opdm_a = Tensor(opdm_a, name='ck_a') opdm_b = Tensor(opdm_b, name='ck_b') tpdm_aa = Tensor(tpdm_aa, name='cckk_aa', basis=bas_aa) tpdm_bb = Tensor(tpdm_bb, name='cckk_bb', basis=bas_aa) tpdm_ab = Tensor(tpdm_ab, name='cckk_ab', basis=bas_ab) tqdm_aa = Tensor(tqdm_aa, name='kkcc_aa', basis=bas_aa) tqdm_bb = Tensor(tqdm_bb, name='kkcc_bb', basis=bas_aa) tqdm_ab = Tensor(tqdm_ab, name='kkcc_ab', basis=bas_ab) rdms = MultiTensor( [opdm_a, opdm_b, tpdm_aa, tpdm_bb, tpdm_ab, tqdm_aa, tqdm_bb, tqdm_ab]) dual_basis = d2_q2_mapping(4) rdms.dual_basis = dual_basis A, _, c = rdms.synthesize_dual_basis() Amat = A.todense() cmat = c.todense() primal_vec = rdms.vectorize_tensors() residual = Amat.dot(primal_vec) - cmat assert np.allclose(residual, np.zeros_like(residual))
def test_d2_spin_rep(): n_density, rdm_generator, transform, molecule = system() assert np.isclose(molecule.fci_energy, -2.84383506834) density = AntiSymmOrbitalDensity(n_density, molecule.n_qubits) tpdm_aa, tpdm_bb, tpdm_ab, _ = density.construct_tpdm() bas_aa, bas_ab = geminal_spin_basis(molecule.n_orbitals) tpdm_ab = Tensor(tpdm_ab, name='cckk_ab', basis=bas_ab) rdms = MultiTensor([tpdm_ab]) dim = int(np.sqrt(tpdm_ab.data.shape[0])) s_rep_dual_constant = 0 for i, j in product(range(dim), repeat=2): s_rep_dual_constant += tpdm_ab.data[bas_ab.rev((i, j)), bas_ab.rev((j, i))] N = molecule.n_electrons M = 0 S = 0 db = DualBasis() db += s_representability_d2ab(dim, N, M, S) rdms.dual_basis = db xvec = rdms.vectorize_tensors() A, _, b = rdms.synthesize_dual_basis() assert np.allclose(A.dot(xvec) - b, 0.0) assert np.allclose(A.dot(xvec), s_rep_dual_constant)
def test_d2_spin_rep_hubbard(): n_density, rdm_generator = system_hubbard() density = AntiSymmOrbitalDensity(n_density, 8) tpdm_aa, tpdm_bb, tpdm_ab, _ = density.construct_tpdm() bas_aa, bas_ab = geminal_spin_basis(4) tpdm_ab = Tensor(tpdm_ab, name='cckk_ab', basis=bas_ab) rdms = MultiTensor([tpdm_ab]) dim = int(np.sqrt(tpdm_ab.data.shape[0])) s_rep_dual_constant = 0 for i, j in product(range(dim), repeat=2): s_rep_dual_constant += tpdm_ab.data[bas_ab.rev((i, j)), bas_ab.rev((j, i))] N = 4 M = 0 S = 0 db = DualBasis() db += s_representability_d2ab(dim, N, M, S) rdms.dual_basis = db xvec = rdms.vectorize_tensors() A, _, b = rdms.synthesize_dual_basis() assert np.allclose(A.dot(xvec) - b, 0.0) assert np.allclose(A.dot(xvec), s_rep_dual_constant)
def test_d2_trace(): n_density, rdm_generator, transform, molecule = system() assert np.isclose(molecule.fci_energy, -2.84383506834) density = AntiSymmOrbitalDensity(n_density, molecule.n_qubits) tpdm_aa, tpdm_bb, tpdm_ab, _ = density.construct_tpdm() bas_aa, bas_ab = geminal_spin_basis(molecule.n_orbitals) tpdm_aa = Tensor(tpdm_aa, name='cckk_aa', basis=bas_aa) tpdm_bb = Tensor(tpdm_bb, name='cckk_bb', basis=bas_aa) tpdm_ab = Tensor(tpdm_ab, name='cckk_ab', basis=bas_ab) rdms = MultiTensor([tpdm_aa, tpdm_bb, tpdm_ab]) dual_basis = trace_d2_aa(molecule.n_orbitals, molecule.n_electrons / 2) rdms.dual_basis = DualBasis(elements=[dual_basis]) A, b, c = rdms.synthesize_dual_basis() Amat = A.todense() bmat = b.todense() cmat = c.todense() primal_vec = rdms.vectorize_tensors() residual = Amat.dot(primal_vec) - cmat assert np.allclose(residual, np.zeros_like(residual)) dual_basis = trace_d2_bb(molecule.n_orbitals, molecule.n_electrons / 2) rdms.dual_basis = DualBasis(elements=[dual_basis]) A, _, c = rdms.synthesize_dual_basis() Amat = A.todense() cmat = c.todense() primal_vec = rdms.vectorize_tensors() residual = Amat.dot(primal_vec) - cmat assert np.allclose(residual, np.zeros_like(residual)) dual_basis = trace_d2_ab(molecule.n_orbitals, molecule.n_electrons / 2, molecule.n_electrons / 2) rdms.dual_basis = DualBasis(elements=[dual_basis]) A, _, c = rdms.synthesize_dual_basis() Amat = A.todense() cmat = c.todense() primal_vec = rdms.vectorize_tensors() residual = Amat.dot(primal_vec) - cmat assert np.allclose(residual, np.zeros_like(residual)) db = DualBasis() db += trace_d2_aa(molecule.n_orbitals, molecule.n_electrons / 2) db += trace_d2_ab(molecule.n_orbitals, molecule.n_electrons / 2, molecule.n_electrons / 2) db += trace_d2_bb(molecule.n_orbitals, molecule.n_electrons / 2) rdms.dual_basis = db A, _, c = rdms.synthesize_dual_basis() Amat = A.todense() cmat = c.todense() primal_vec = rdms.vectorize_tensors() residual = Amat.dot(primal_vec) - cmat assert np.allclose(residual, np.zeros_like(residual))
def test_d2_d1_mapping_hubbard(): n_density, rdm_generator = system_hubbard() density = AntiSymmOrbitalDensity(n_density, 8) tpdm_aa, tpdm_bb, tpdm_ab, [bas_aa, bas_ab] = density.construct_tpdm() opdm_a, opdm_b = density.construct_opdm() from itertools import product test_opdm = np.zeros_like(opdm_a) for i, j in product(range(opdm_a.shape[0]), repeat=2): if i <= j: for r in range(opdm_a.shape[0]): if i != r and j != r: top_gem = tuple(sorted([i, r])) bot_gem = tuple(sorted([j, r])) parity = (-1)**(r < i) * (-1)**(r < j) if i == j: test_opdm[i, j] += tpdm_aa[bas_aa[top_gem], bas_aa[bot_gem]] * parity else: test_opdm[j, i] += tpdm_aa[bas_aa[top_gem], bas_aa[bot_gem]] * parity * 0.5 test_opdm[j, i] += tpdm_aa[bas_aa[bot_gem], bas_aa[top_gem]] * parity * 0.5 test_opdm[i, j] += tpdm_aa[bas_aa[top_gem], bas_aa[bot_gem]] * parity * 0.5 test_opdm[i, j] += tpdm_aa[bas_aa[bot_gem], bas_aa[top_gem]] * parity * 0.5 assert np.allclose(test_opdm, opdm_a) bas_aa, bas_ab = geminal_spin_basis(4) opdm_a = Tensor(opdm_a, name='ck_a') opdm_b = Tensor(opdm_b, name='ck_b') tpdm_aa = Tensor(tpdm_aa, name='cckk_aa', basis=bas_aa) tpdm_bb = Tensor(tpdm_bb, name='cckk_bb', basis=bas_aa) tpdm_ab = Tensor(tpdm_ab, name='cckk_ab', basis=bas_ab) rdms = MultiTensor([opdm_a, opdm_b, tpdm_aa, tpdm_bb, tpdm_ab]) # d2ab_d1a test Na = Nb = 2 dual_basis = d2ab_d1a_mapping(4, Nb) dual_basis += d2ab_d1b_mapping(4, Na) dual_basis += d2aa_d1a_mapping(4, Na) dual_basis += d2bb_d1b_mapping(4, Nb) rdms.dual_basis = dual_basis A, _, c = rdms.synthesize_dual_basis() Amat = A.todense() w, v = np.linalg.eigh(np.dot(Amat, Amat.T)) cmat = c.todense() primal_vec = rdms.vectorize_tensors() residual = Amat.dot(primal_vec) - cmat assert np.allclose(residual, np.zeros_like(residual))
def test_d2_trace_hubbard(): n_density, rdm_generator = system_hubbard() density = AntiSymmOrbitalDensity(n_density, 8) tpdm_aa, tpdm_bb, tpdm_ab, _ = density.construct_tpdm() bas_aa, bas_ab = geminal_spin_basis(4) tpdm_aa = Tensor(tpdm_aa, name='cckk_aa', basis=bas_aa) tpdm_bb = Tensor(tpdm_bb, name='cckk_bb', basis=bas_aa) tpdm_ab = Tensor(tpdm_ab, name='cckk_ab', basis=bas_ab) rdms = MultiTensor([tpdm_aa, tpdm_bb, tpdm_ab]) dual_basis = trace_d2_aa(4, 2) rdms.dual_basis = DualBasis(elements=[dual_basis]) A, b, c = rdms.synthesize_dual_basis() Amat = A.todense() bmat = b.todense() cmat = c.todense() primal_vec = rdms.vectorize_tensors() residual = Amat.dot(primal_vec) - cmat assert np.allclose(residual, np.zeros_like(residual)) dual_basis = trace_d2_bb(4, 2) rdms.dual_basis = DualBasis(elements=[dual_basis]) A, _, c = rdms.synthesize_dual_basis() Amat = A.todense() cmat = c.todense() primal_vec = rdms.vectorize_tensors() residual = Amat.dot(primal_vec) - cmat assert np.allclose(residual, np.zeros_like(residual)) dual_basis = trace_d2_ab(4, 2, 2) rdms.dual_basis = DualBasis(elements=[dual_basis]) A, _, c = rdms.synthesize_dual_basis() Amat = A.todense() cmat = c.todense() primal_vec = rdms.vectorize_tensors() residual = Amat.dot(primal_vec) - cmat assert np.allclose(residual, np.zeros_like(residual)) db = DualBasis() db += trace_d2_aa(4, 2) db += trace_d2_ab(4, 2, 2) db += trace_d2_bb(4, 2) rdms.dual_basis = db A, _, c = rdms.synthesize_dual_basis() Amat = A.todense() cmat = c.todense() primal_vec = rdms.vectorize_tensors() residual = Amat.dot(primal_vec) - cmat assert np.allclose(residual, np.zeros_like(residual))
def system_h4(): print('Running System Setup H4') basis = 'sto-3g' multiplicity = 1 charge = 0 r = 0.75 geometry = [('H', [0.0, 0.0, 0.0]), ('H', [0, 0, r]), ('H', [0, 0, 2 * r]), ('H', [0, 0, 3 * r])] molecule = MolecularData(geometry, basis, multiplicity, charge) # Run Psi4. molecule = run_psi4(molecule, run_scf=True, run_mp2=False, run_cisd=False, run_ccsd=False, run_fci=True, delete_input=False) op_mat = of.get_sparse_operator( molecule.get_molecular_hamiltonian()).toarray() w, v = np.linalg.eigh(op_mat) n_density = v[:, [0]] @ v[:, [0]].conj().T rdm_generator = AntiSymmOrbitalDensity(n_density, molecule.n_qubits) transform = jordan_wigner return n_density, rdm_generator, transform, molecule
def system_hubbard(): print('Running System Setup') import openfermion as of U = 1 sites = 4 hubbard = of.hamiltonians.fermi_hubbard(1, sites, tunneling=1, coulomb=U, chemical_potential=0, magnetic_field=0, periodic=False, spinless=False) # hamiltonian = of.get_interaction_operator(hubbard) op_mat = of.get_number_preserving_sparse_operator( hubbard, 8, 4, spin_preserving=False).toarray() w, v = np.linalg.eigh(op_mat) gs_e = w[0] print(gs_e) op_mat = of.get_sparse_operator(hubbard).toarray() w, v = np.linalg.eigh(op_mat) print(w[0]) n_density = v[:, [0]] @ v[:, [0]].conj().T rdm_generator = AntiSymmOrbitalDensity(n_density, sites * 2) return n_density, rdm_generator
def test_d1_q1_mapping(): n_density, rdm_generator, transform, molecule = system_h4() density = AntiSymmOrbitalDensity(n_density, molecule.n_qubits) tpdm_aa, tpdm_bb, tpdm_ab, _ = density.construct_tpdm() opdm_a, opdm_b = density.construct_opdm() oqdm_a, oqdm_b = density.construct_ohdm() bas_aa, bas_ab = geminal_spin_basis(molecule.n_orbitals) opdm_a = Tensor(opdm_a, name='ck_a') opdm_b = Tensor(opdm_b, name='ck_b') oqdm_a = Tensor(oqdm_a, name='kc_a') oqdm_b = Tensor(oqdm_b, name='kc_b') rdms = MultiTensor([opdm_a, opdm_b, oqdm_a, oqdm_b]) dual_basis = d1a_q1a_mapping(molecule.n_orbitals) rdms.dual_basis = dual_basis A, _, c = rdms.synthesize_dual_basis() Amat = A.todense() cmat = c.todense() primal_vec = rdms.vectorize_tensors() residual = Amat.dot(primal_vec) - cmat assert np.allclose(residual, np.zeros_like(residual)) dual_basis = d1b_q1b_mapping(molecule.n_orbitals) rdms.dual_basis = dual_basis A, _, c = rdms.synthesize_dual_basis() Amat = A.todense() cmat = c.todense() primal_vec = rdms.vectorize_tensors() residual = Amat.dot(primal_vec) - cmat assert np.allclose(residual, np.zeros_like(residual)) dual_basis_a = d1b_q1b_mapping(molecule.n_orbitals) dual_basis_b = d1a_q1a_mapping(molecule.n_orbitals) rdms.dual_basis = dual_basis_a + dual_basis_b A, _, c = rdms.synthesize_dual_basis() Amat = A.todense() cmat = c.todense() primal_vec = rdms.vectorize_tensors() residual = Amat.dot(primal_vec) - cmat assert np.allclose(residual, np.zeros_like(residual))
def test_unspin_adapt_1(): heh_file = os.path.join(DATA_DIRECTORY, 'H1-He1_sto-3g_singlet_1+_0.74.hdf5') molecule = MolecularData(filename=heh_file) tpdm_from_of = np.einsum('ijkl->ijlk', molecule.fci_two_rdm) rho, rdm_generator, transform, mol2 = system() assert np.allclose(np.conj(rho).T, rho) dim = molecule.n_qubits rdm_generator_antisym = AntiSymmOrbitalDensity(rho, molecule.n_qubits) tpdm_aa, tpdm_bb, tpdm_ab, [bas_aa, bas_ab ] = rdm_generator_antisym.construct_tpdm() assert np.allclose(opdm, molecule.fci_one_rdm) assert np.allclose(tpdm, tpdm_from_of) tpdm_test = unspin_adapt(tpdm_aa, tpdm_bb, tpdm_ab) # print(np.linalg.norm(tpdm[::2, ::2, ::2, ::2] - tpdm_test[::2, ::2, ::2, ::2])) np.testing.assert_allclose(tpdm[::2, ::2, ::2, ::2], tpdm_test[::2, ::2, ::2, ::2]) # print(np.linalg.norm(tpdm[1::2, 1::2, 1::2, 1::2] - tpdm_test[1::2, 1::2, 1::2, 1::2])) np.testing.assert_allclose(tpdm[1::2, 1::2, 1::2, 1::2], tpdm_test[1::2, 1::2, 1::2, 1::2]) # print(np.linalg.norm(tpdm[::2, 1::2, ::2, 1::2] - tpdm_test[::2, 1::2, ::2, 1::2])) np.testing.assert_allclose(tpdm[::2, 1::2, ::2, 1::2], tpdm_test[::2, 1::2, ::2, 1::2]) # print(np.linalg.norm(tpdm[1::2, ::2, 1::2, ::2] - tpdm_test[1::2, ::2, 1::2, ::2])) np.testing.assert_allclose(tpdm[1::2, ::2, 1::2, ::2], tpdm_test[1::2, ::2, 1::2, ::2]) for p, q, r, s in product(range(tpdm.shape[0]), repeat=4): if not np.isclose(tpdm[p, q, r, s], tpdm_test[p, q, r, s]): print((p, q, r, s), tpdm[p, q, r, s], tpdm_test[p, q, r, s]) heh_file = os.path.join(DATA_DIRECTORY, 'H1-He1_sto-3g_singlet_1+_0.74.hdf5') molecule = MolecularData(filename=heh_file) tpdm_from_of = np.einsum('ijkl->ijlk', molecule.fci_two_rdm) for p, q, r, s in product(range(tpdm.shape[0]), repeat=4): if not np.isclose(tpdm_from_of[p, q, r, s], tpdm[p, q, r, s]): print((p, q, r, s), tpdm_from_of[p, q, r, s], tpdm[p, q, r, s]) t_tpdm_aa, t_tpdm_bb, t_tpdm_ab = get_sz_spin_adapted(tpdm) assert np.allclose(t_tpdm_ab, tpdm_ab)
def system(): print('Running System Setup') basis = 'sto-3g' multiplicity = 1 charge = 1 geometry = [('He', [0.0, 0.0, 0.0]), ('H', [0, 0, 0.740848149])] molecule = MolecularData(geometry, basis, multiplicity, charge) # Run Psi4. molecule = run_psi4(molecule, run_scf=True, run_mp2=False, run_cisd=False, run_ccsd=False, run_fci=True, delete_input=False) molecule, gs_wf, n_density, eigen_val = get_molecule_openfermion( molecule, eigen_index=2) rdm_generator = AntiSymmOrbitalDensity(n_density, molecule.n_qubits) transform = jordan_wigner return n_density, rdm_generator, transform, molecule
def test_d2_g2_mapping(): n_density, rdm_generator, transform, molecule = system_h4() density = AntiSymmOrbitalDensity(n_density, molecule.n_qubits) tpdm_aa, tpdm_bb, tpdm_ab, _ = density.construct_tpdm() tqdm_aa, tqdm_bb, tqdm_ab, _ = density.construct_thdm() phdm_ab, phdm_ba, phdm_aabb = density.construct_phdm() opdm_a, opdm_b = density.construct_opdm() bas_aa, bas_ab = geminal_spin_basis(molecule.n_orbitals) opdm_a = Tensor(opdm_a, name='ck_a') opdm_b = Tensor(opdm_b, name='ck_b') tpdm_aa = Tensor(tpdm_aa, name='cckk_aa', basis=bas_aa) tpdm_bb = Tensor(tpdm_bb, name='cckk_bb', basis=bas_aa) tpdm_ab = Tensor(tpdm_ab, name='cckk_ab', basis=bas_ab) tqdm_aa = Tensor(tqdm_aa, name='kkcc_aa', basis=bas_aa) tqdm_bb = Tensor(tqdm_bb, name='kkcc_bb', basis=bas_aa) tqdm_ab = Tensor(tqdm_ab, name='kkcc_ab', basis=bas_ab) phdm_ab = Tensor(phdm_ab, name='ckck_ab') phdm_ba = Tensor(phdm_ba, name='ckck_ba') phdm_aabb = Tensor( phdm_aabb, name='ckck_aabb' ) # What basis do we want to use for super blocks like this? rdms = MultiTensor([ opdm_a, opdm_b, tpdm_aa, tpdm_bb, tpdm_ab, tqdm_aa, tqdm_bb, tqdm_ab, phdm_ab, phdm_ba, phdm_aabb ]) dual_basis = d2_g2_mapping(molecule.n_orbitals) rdms.dual_basis = dual_basis A, _, c = rdms.synthesize_dual_basis() Amat = A.todense() cmat = c.todense() primal_vec = rdms.vectorize_tensors() residual = Amat.dot(primal_vec) - cmat assert np.allclose(residual, np.zeros_like(residual))
def test_d2_e2_constraint(): n_density, rdm_generator, transform, molecule = system() density = AntiSymmOrbitalDensity(n_density, molecule.n_qubits) tpdm_aa, tpdm_bb, tpdm_ab, [bas_aa, bas_ab] = density.construct_tpdm() tqdm_aa, tqdm_bb, tqdm_ab, _ = density.construct_thdm() # generate error tensors corrupted_tpdm_aa = add_gaussian_noise(tpdm_aa, 1) corrupted_tpdm_aa = 0.5 * (corrupted_tpdm_aa + corrupted_tpdm_aa.T) corrupted_tpdm_bb = add_gaussian_noise(tpdm_bb, 1) corrupted_tpdm_bb = 0.5 * (corrupted_tpdm_bb + corrupted_tpdm_bb.T) corrupted_tpdm_ab = add_gaussian_noise(tpdm_ab, 1) corrupted_tpdm_ab = 0.5 * (corrupted_tpdm_ab + corrupted_tpdm_ab.T) error_aa = corrupted_tpdm_aa - tpdm_aa error_bb = corrupted_tpdm_bb - tpdm_bb error_ab = corrupted_tpdm_ab - tpdm_ab error_matrix_aa = density.construct_tpdm_error_matrix(error_aa) error_matrix_bb = density.construct_tpdm_error_matrix(error_bb) error_matrix_ab = density.construct_tpdm_error_matrix(error_ab) assert error_matrix_aa.shape[0] == 2 * tpdm_aa.shape[0] assert error_matrix_bb.shape[0] == 2 * tpdm_bb.shape[0] assert error_matrix_ab.shape[0] == 2 * tpdm_ab.shape[0] dim_aa = tpdm_aa.shape[0] dim_bb = tpdm_bb.shape[0] dim_ab = tpdm_ab.shape[0] assert np.allclose(error_matrix_aa[:dim_aa, :dim_aa], np.eye(dim_aa)) assert np.allclose(error_matrix_bb[:dim_bb, :dim_bb], np.eye(dim_bb)) assert np.allclose(error_matrix_ab[:dim_ab, :dim_ab], np.eye(dim_ab)) assert np.allclose(error_matrix_aa[:dim_aa, dim_aa:], error_aa) assert np.allclose(error_matrix_aa[dim_aa:, :dim_aa], error_aa.T) assert np.allclose(error_matrix_bb[:dim_bb, dim_bb:], error_bb) assert np.allclose(error_matrix_bb[dim_bb:, :dim_bb], error_bb.T) assert np.allclose(error_matrix_ab[:dim_ab, dim_ab:], error_ab) assert np.allclose(error_matrix_ab[dim_ab:, :dim_ab], error_ab.T) # get basis bijection bij_bas_aa, bij_bas_ab = geminal_spin_basis(molecule.n_orbitals) tpdm_aa = Tensor(tensor=tpdm_aa, name='cckk_aa', basis=bij_bas_aa) error_tensor_aa = Tensor(tensor=error_matrix_aa, name='cckk_me_aa') tpdm_bb = Tensor(tensor=tpdm_bb, name='cckk_bb', basis=bij_bas_aa) error_tensor_bb = Tensor(tensor=error_matrix_bb, name='cckk_me_bb') tpdm_ab = Tensor(tensor=tpdm_ab, name='cckk_ab', basis=bij_bas_ab) error_tensor_ab = Tensor(tensor=error_matrix_ab, name='cckk_me_ab') rdms = MultiTensor([ tpdm_aa, tpdm_bb, tpdm_ab, error_tensor_aa, error_tensor_bb, error_tensor_ab ]) xvec = rdms.vectorize_tensors() db = d2_e2_mapping(int(np.sqrt(dim_ab)), bas_aa, bas_ab, corrupted_tpdm_aa, corrupted_tpdm_bb, corrupted_tpdm_ab) rdms.dual_basis = db A, _, b = rdms.synthesize_dual_basis() A = A.todense() b = b.todense() w, v = np.linalg.eigh(A.dot(A.T)) assert np.all(w > 0) assert np.allclose(A.dot(xvec) - b, 0.0)
def test_d2_g2_mapping(): n_density, rdm_generator, transform, molecule = system_h4() # n_density, rdm_generator = system_hubbard() # n_density, rdm_generator, transform, molecule = system() dim = 4 density = AntiSymmOrbitalDensity(n_density, 2 * dim) tpdm_aa, tpdm_bb, tpdm_ab, _ = density.construct_tpdm() tqdm_aa, tqdm_bb, tqdm_ab, _ = density.construct_thdm() phdm_ab, phdm_ba, phdm_aabb = density.construct_phdm() # for i, j, k, l in product(range(dim), repeat=4): # fop = ((2 * i, 1), (2 * j + 1, 0), (2 * l + 1, 1), (2 * k, 0)) # fop = of.FermionOperator(fop) # opmat = of.get_sparse_operator(fop, n_qubits=2 * dim) # rdm_val = (np.trace(n_density @ opmat)) # # print(rdm_val, phdm_ab[i * dim + j, k * dim + l]) # assert np.isclose(rdm_val, phdm_ab[i * dim + j, k * dim + l]) # for i, j, k, l in product(range(dim), repeat=4): # fop = ((2 * i + 1, 1), (2 * j, 0), (2 * l, 1), (2 * k + 1, 0)) # fop = of.FermionOperator(fop) # opmat = of.get_sparse_operator(fop, n_qubits=2 * dim) # rdm_val = (np.trace(n_density @ opmat)) # assert np.isclose(rdm_val, phdm_ba[i * dim + j, k * dim + l]) # for i, j, k, l in product(range(dim), repeat=4): # fop = ((2 * i, 1), (2 * j, 0), (2 * k, 1), (2 * l, 0)) # fop = of.FermionOperator(fop) # opmat = of.get_sparse_operator(of.jordan_wigner(fop), n_qubits=2 * dim) # rdm_val = (np.trace(n_density @ opmat)) # assert np.isclose(rdm_val, phdm_aabb[i * dim + j, l * dim + k]) # for i, j, k, l in product(range(dim), repeat=4): # fop = ((2 * i + 1, 1), (2 * j + 1, 0), (2 * k + 1, 1), (2 * l + 1, 0)) # fop = of.FermionOperator(fop) # opmat = of.get_sparse_operator(of.jordan_wigner(fop), n_qubits=2 * dim) # rdm_val = (np.trace(n_density @ opmat)) # # print((i, j, k, l), rdm_val, phdm_aabb[i * dim + j + dim**2, l * dim + k + dim**2]) # assert np.isclose(rdm_val, phdm_aabb[i * dim + j + dim**2, l * dim + k + dim**2]) # for i, j, k, l in product(range(dim), repeat=4): # fop = ((2 * i, 1), (2 * j, 0), (2 * k + 1, 1), (2 * l + 1, 0)) # fop = of.FermionOperator(fop) # opmat = of.get_sparse_operator(of.jordan_wigner(fop), n_qubits=2 * dim) # rdm_val = (np.trace(n_density @ opmat)) # assert np.isclose(rdm_val, phdm_aabb[i * dim + j, l * dim + k + dim**2]) # for i, j, k, l in product(range(dim), repeat=4): # fop = ((2 * i + 1, 1), (2 * j + 1, 0), (2 * l, 1), (2 * k, 0)) # fop = of.FermionOperator(fop) # opmat = of.get_sparse_operator(of.jordan_wigner(fop), n_qubits=2 * dim) # rdm_val = (np.trace(n_density @ opmat)) # # print((i, j, k, l), rdm_val, phdm_aabb[i * dim + j + dim**2, k * dim + l]) # assert np.isclose(rdm_val, phdm_aabb[i * dim + j + dim**2, k * dim + l]) # for i, j, k, l in product(range(dim), repeat=4): # assert np.isclose(phdm_aabb[i * dim + j + dim**2, k * dim + l], # phdm_aabb[k * dim + l, i * dim + j + dim**2] # ) assert of.is_hermitian(phdm_ab) assert of.is_hermitian(phdm_ba) assert of.is_hermitian(phdm_aabb) w, v = np.linalg.eigh(phdm_ab) assert np.all(w > -1.0E-14) w, v = np.linalg.eigh(phdm_ba) assert np.all(w > -1.0E-14) w, v = np.linalg.eigh(phdm_aabb) assert np.all(w > -1.0E-14) opdm_a, opdm_b = density.construct_opdm() bas_aa, bas_ab = geminal_spin_basis(dim) opdm_a = Tensor(opdm_a, name='ck_a') opdm_b = Tensor(opdm_b, name='ck_b') tpdm_aa = Tensor(tpdm_aa, name='cckk_aa', basis=bas_aa) tpdm_bb = Tensor(tpdm_bb, name='cckk_bb', basis=bas_aa) tpdm_ab = Tensor(tpdm_ab, name='cckk_ab', basis=bas_ab) tqdm_aa = Tensor(tqdm_aa, name='kkcc_aa', basis=bas_aa) tqdm_bb = Tensor(tqdm_bb, name='kkcc_bb', basis=bas_aa) tqdm_ab = Tensor(tqdm_ab, name='kkcc_ab', basis=bas_ab) phdm_ab = Tensor(phdm_ab, name='ckck_ab', basis=bas_ab) phdm_ba = Tensor(phdm_ba, name='ckck_ba', basis=bas_ab) phdm_aabb = Tensor( phdm_aabb, name='ckck_aabb' ) # What basis do we want to use for super blocks like this? rdms = MultiTensor([ opdm_a, opdm_b, tpdm_aa, tpdm_bb, tpdm_ab, tqdm_aa, tqdm_bb, tqdm_ab, phdm_ab, phdm_ba, phdm_aabb ]) dual_basis = d2_g2_mapping(dim) rdms.dual_basis = dual_basis A, _, c = rdms.synthesize_dual_basis() Amat = A.todense() cmat = c.todense() primal_vec = rdms.vectorize_tensors() residual = Amat.dot(primal_vec) - cmat assert np.allclose(residual, np.zeros_like(residual))
def test_d2_trace_h4(): n_density, rdm_generator, transform, molecule = system_h4() density = AntiSymmOrbitalDensity(n_density, molecule.n_qubits) dim = molecule.n_orbitals tpdm_aa, tpdm_bb, tpdm_ab, _ = density.construct_tpdm() Na, Nb = 2, 2 trace_ab = 0 for i, j in product(range(molecule.n_orbitals), repeat=2): trace_ab += tpdm_ab[i * dim + j, i * dim + j] assert np.isclose(trace_ab, Na, Nb) assert np.isclose(np.trace(tpdm_aa), Na * (Na - 1) / 2) assert np.isclose(np.trace(tpdm_bb), Nb * (Nb - 1) / 2) bas_aa, bas_ab = geminal_spin_basis(molecule.n_orbitals) tpdm_aa = Tensor(tpdm_aa, name='cckk_aa', basis=bas_aa) tpdm_bb = Tensor(tpdm_bb, name='cckk_bb', basis=bas_aa) tpdm_ab = Tensor(tpdm_ab, name='cckk_ab', basis=bas_ab) rdms = MultiTensor([tpdm_aa, tpdm_bb, tpdm_ab]) dual_basis = trace_d2_aa(molecule.n_orbitals, molecule.n_electrons / 2) rdms.dual_basis = DualBasis(elements=[dual_basis]) A, b, c = rdms.synthesize_dual_basis() Amat = A.todense() bmat = b.todense() cmat = c.todense() primal_vec = rdms.vectorize_tensors() residual = Amat.dot(primal_vec) - cmat assert np.allclose(residual, np.zeros_like(residual)) dual_basis = trace_d2_bb(molecule.n_orbitals, molecule.n_electrons / 2) rdms.dual_basis = DualBasis(elements=[dual_basis]) A, _, c = rdms.synthesize_dual_basis() Amat = A.todense() cmat = c.todense() primal_vec = rdms.vectorize_tensors() residual = Amat.dot(primal_vec) - cmat assert np.allclose(residual, np.zeros_like(residual)) dual_basis = trace_d2_ab(molecule.n_orbitals, molecule.n_electrons / 2, molecule.n_electrons / 2) rdms.dual_basis = DualBasis(elements=[dual_basis]) A, _, c = rdms.synthesize_dual_basis() Amat = A.todense() cmat = c.todense() primal_vec = rdms.vectorize_tensors() residual = Amat.dot(primal_vec) - cmat assert np.allclose(residual, np.zeros_like(residual)) db = DualBasis() db += trace_d2_aa(molecule.n_orbitals, molecule.n_electrons / 2) db += trace_d2_ab(molecule.n_orbitals, molecule.n_electrons / 2, molecule.n_electrons / 2) db += trace_d2_bb(molecule.n_orbitals, molecule.n_electrons / 2) rdms.dual_basis = db A, _, c = rdms.synthesize_dual_basis() Amat = A.todense() cmat = c.todense() primal_vec = rdms.vectorize_tensors() residual = Amat.dot(primal_vec) - cmat assert np.allclose(residual, np.zeros_like(residual))
def dqg_run_bpsdp(): import sys from openfermion.hamiltonians import MolecularData from openfermionpsi4 import run_psi4 from openfermionpyscf import run_pyscf from openfermion.utils import map_one_pdm_to_one_hole_dm, \ map_two_pdm_to_two_hole_dm, map_two_pdm_to_particle_hole_dm print('Running System Setup') basis = 'sto-6g' # basis = '6-31g' multiplicity = 1 # charge = 0 # geometry = [('H', [0.0, 0.0, 0.0]), ('H', [0, 0, 0.75])] # charge = 1 # geometry = [('H', [0.0, 0.0, 0.0]), ('He', [0, 0, 0.75])] charge = 0 bd = 1.2 # geometry = [('H', [0.0, 0.0, 0.0]), ('H', [0, 0, bd]), # ('H', [0.0, 0.0, 2 * bd]), ('H', [0, 0, 3 * bd])] # geometry = [['H', [0, 0, 0]], ['H', [1.2, 0, 0]], # ['H', [0, 1.2, 0]], ['H', [1.2, 1.2, 0]]] # geometry = [['He', [0, 0, 0]], ['H', [0, 0, 1.2]]] # geometry = [['Be' [0, 0, 0]], [['B', [1.2, 0, 0]]]] geometry = [['N', [0, 0, 0]], ['N', [0, 0, 1.1]]] molecule = MolecularData(geometry, basis, multiplicity, charge) # Run Psi4. # molecule = run_psi4(molecule, # run_scf=True, # run_mp2=False, # run_cisd=False, # run_ccsd=False, # run_fci=True, # delete_input=True) molecule = run_pyscf(molecule, run_scf=True, run_mp2=False, run_cisd=False, run_ccsd=False, run_fci=True) print('nuclear_repulsion', molecule.nuclear_repulsion) print('gs energy ', molecule.fci_energy) print("hf energy ", molecule.hf_energy) nuclear_repulsion = molecule.nuclear_repulsion gs_energy = molecule.fci_energy import openfermion as of hamiltonian = molecule.get_molecular_hamiltonian( occupied_indices=[0], active_indices=[1, 2, 3, 4]) print(type(hamiltonian)) print(hamiltonian) nuclear_repulsion = hamiltonian.constant hamiltonian.constant = 0 ham = of.get_sparse_operator(hamiltonian).toarray() w, v = np.linalg.eigh(ham) idx = 0 gs_energy = w[idx] n_density = v[:, [idx]] @ v[:, [idx]].conj().T from representability.fermions.density.antisymm_sz_density import AntiSymmOrbitalDensity density = AntiSymmOrbitalDensity(n_density, 8) opdm_a, opdm_b = density.construct_opdm() tpdm_aa, tpdm_bb, tpdm_ab, _ = density.construct_tpdm() true_tpdm = density.get_tpdm(density.rho, density.dim) true_tpdm = true_tpdm.transpose(0, 1, 3, 2) test_tpdm = unspin_adapt(tpdm_aa, tpdm_bb, tpdm_ab) assert np.allclose(true_tpdm, test_tpdm) tqdm_aa, tqdm_bb, tqdm_ab, _ = density.construct_thdm() phdm_ab, phdm_ba, phdm_aabb = density.construct_phdm() Na = np.round(opdm_a.trace()).real Nb = np.round(opdm_b.trace()).real one_body_ints, two_body_ints = hamiltonian.one_body_tensor, hamiltonian.two_body_tensor two_body_ints = np.einsum('ijkl->ijlk', two_body_ints) n_electrons = Na + Nb print('n_electrons', n_electrons) dim = one_body_ints.shape[0] spatial_basis_rank = dim // 2 bij_bas_aa, bij_bas_ab = geminal_spin_basis(spatial_basis_rank) opdm_a_interaction, opdm_b_interaction, v2aa, v2bb, v2ab = \ spin_adapted_interaction_tensor_rdm_consistent(two_body_ints, one_body_ints) dual_basis = sz_adapted_linear_constraints( spatial_basis_rank, Na, Nb, ['ck', 'kc', 'cckk', 'ckck', 'kkcc'], S=1, M=-1) print("constructed dual basis") opdm_a = Tensor(opdm_a, name='ck_a') opdm_b = Tensor(opdm_b, name='ck_b') oqdm_a = Tensor(np.eye(dim // 2) - opdm_a.data, name='kc_a') oqdm_b = Tensor(np.eye(dim // 2) - opdm_b.data, name='kc_b') tpdm_aa = Tensor(tpdm_aa, name='cckk_aa', basis=bij_bas_aa) tpdm_bb = Tensor(tpdm_bb, name='cckk_bb', basis=bij_bas_aa) tpdm_ab = Tensor(tpdm_ab, name='cckk_ab', basis=bij_bas_ab) tqdm_aa = Tensor(tqdm_aa, name='kkcc_aa', basis=bij_bas_aa) tqdm_bb = Tensor(tqdm_bb, name='kkcc_bb', basis=bij_bas_aa) tqdm_ab = Tensor(tqdm_ab, name='kkcc_ab', basis=bij_bas_ab) phdm_ab = Tensor(phdm_ab, name='ckck_ab', basis=bij_bas_ab) phdm_ba = Tensor(phdm_ba, name='ckck_ba', basis=bij_bas_ab) phdm_aabb = Tensor(phdm_aabb, name='ckck_aabb') dtensor = MultiTensor([ opdm_a, opdm_b, oqdm_a, oqdm_b, tpdm_aa, tpdm_bb, tpdm_ab, tqdm_aa, tqdm_bb, tqdm_ab, phdm_ab, phdm_ba, phdm_aabb ]) copdm_a = opdm_a_interaction copdm_b = opdm_b_interaction coqdm_a = Tensor(np.zeros((spatial_basis_rank, spatial_basis_rank)), name='kc_a') coqdm_b = Tensor(np.zeros((spatial_basis_rank, spatial_basis_rank)), name='kc_b') ctpdm_aa = v2aa ctpdm_bb = v2bb ctpdm_ab = v2ab ctqdm_aa = Tensor(np.zeros_like(v2aa.data), name='kkcc_aa', basis=bij_bas_aa) ctqdm_bb = Tensor(np.zeros_like(v2bb.data), name='kkcc_bb', basis=bij_bas_aa) ctqdm_ab = Tensor(np.zeros_like(v2ab.data), name='kkcc_ab', basis=bij_bas_ab) cphdm_ab = Tensor(np.zeros((spatial_basis_rank**2, spatial_basis_rank**2)), name='ckck_ab', basis=bij_bas_ab) cphdm_ba = Tensor(np.zeros((spatial_basis_rank**2, spatial_basis_rank**2)), name='ckck_ba', basis=bij_bas_ab) cphdm_aabb = Tensor(np.zeros( (2 * spatial_basis_rank**2, 2 * spatial_basis_rank**2)), name='ckck_aabb') ctensor = MultiTensor([ copdm_a, copdm_b, coqdm_a, coqdm_b, ctpdm_aa, ctpdm_bb, ctpdm_ab, ctqdm_aa, ctqdm_bb, ctqdm_ab, cphdm_ab, cphdm_ba, cphdm_aabb ]) print( (ctensor.vectorize_tensors().T @ dtensor.vectorize_tensors())[0, 0].real) print(gs_energy) ctensor.dual_basis = dual_basis A, _, b = ctensor.synthesize_dual_basis() print("size of dual basis", len(dual_basis.elements)) print(A @ dtensor.vectorize_tensors() - b) nc, nv = A.shape A.eliminate_zeros() nnz = A.nnz from sdpsolve.sdp import SDP from sdpsolve.solvers.bpsdp import solve_bpsdp from sdpsolve.solvers.bpsdp.bpsdp_old import solve_bpsdp from sdpsolve.utils.matreshape import vec2block sdp = SDP() sdp.nc = nc sdp.nv = nv sdp.nnz = nnz sdp.blockstruct = list(map(lambda x: int(np.sqrt(x.size)), ctensor.tensors)) sdp.nb = len(sdp.blockstruct) sdp.Amat = A.real sdp.bvec = b.todense().real sdp.cvec = ctensor.vectorize_tensors().real sdp.Initialize() epsilon = 1.0E-7 sdp.epsilon = float(epsilon) sdp.epsilon_inner = float(epsilon) / 100 sdp.disp = True sdp.iter_max = 70000 sdp.inner_solve = 'CG' sdp.inner_iter_max = 2 # # sdp_data = solve_bpsdp(sdp) solve_bpsdp(sdp) # # create all the psd-matrices for the # variable_dictionary = {} # for tensor in ctensor.tensors: # linear_dim = int(np.sqrt(tensor.size)) # variable_dictionary[tensor.name] = cvx.Variable(shape=(linear_dim, linear_dim), PSD=True, name=tensor.name) # print("constructing constraints") # constraints = [] # for dbe in dual_basis: # single_constraint = [] # for tname, v_elements, p_coeffs in dbe: # active_indices = get_var_indices(ctensor.tensors[tname], v_elements) # single_constraint.append(variable_dictionary[tname][active_indices] * p_coeffs) # constraints.append(cvx.sum(single_constraint) == dbe.dual_scalar) # print('constraints constructed') # print("constructing the problem") # objective = cvx.Minimize( # cvx.trace(copdm_a.data @ variable_dictionary['ck_a']) + # cvx.trace(copdm_b.data @ variable_dictionary['ck_b']) + # cvx.trace(ctpdm_aa.data @ variable_dictionary['cckk_aa']) + # cvx.trace(ctpdm_bb.data @ variable_dictionary['cckk_bb']) + # cvx.trace(ctpdm_ab.data @ variable_dictionary['cckk_ab'])) # cvx_problem = cvx.Problem(objective, constraints=constraints) # print('problem constructed') # cvx_problem.solve(solver=cvx.SCS, verbose=True, eps=0.5E-5, max_iters=100000) # rdms_solution = vec2block(sdp.blockstruct, sdp.primal) print(gs_energy) # print(cvx_problem.value + nuclear_repulsion) # print(sdp_data.primal_value() + nuclear_repulsion) print(sdp.primal.T @ sdp.cvec) print(nuclear_repulsion) rdms = vec2block(sdp.blockstruct, sdp.primal) tpdm = unspin_adapt(rdms[4], rdms[5], rdms[6]) print(np.einsum('ijij', tpdm)) tpdm = np.einsum('ijkl->ijlk', tpdm)