|
| 1 | +import cudaq, cudaqlib |
| 2 | + |
| 3 | +# Define the molecule |
| 4 | +distance = 1.1621 |
| 5 | +geometry = [('O', (0., 0., 0.)), ('C', (0., 0., distance)), ('O', (0., 0., 2*distance))] |
| 6 | +molecule = cudaqlib.operators.create_molecule(geometry, 'sto-3g', 0, 0, MP2=True, nele_cas=10, norb_cas=9) |
| 7 | + |
| 8 | +# Get the system Hamiltonian |
| 9 | +hamiltonian = molecule.hamiltonian |
| 10 | + |
| 11 | +# Get the number of qubits |
| 12 | +numQubits = molecule.hamiltonian.get_qubit_count() |
| 13 | + |
| 14 | +# Create the operator pool |
| 15 | +pool = cudaqlib.gse.get_operator_pool('uccsd', |
| 16 | + num_qubits=numQubits, |
| 17 | + num_electrons=10, |
| 18 | + operator_coeffs=[0.003125, -0.003125, 0.00625, -0.00625, 0.0125, -0.0125, 0.025, -0.025, 0.05, -0.05, 0.1, -0.1]) |
| 19 | + |
| 20 | +# Define Hartree-Fock |
| 21 | +@cudaq.kernel |
| 22 | +def init(q: cudaq.qview): |
| 23 | + for i in range(10): |
| 24 | + x(q[i]) |
| 25 | + |
| 26 | + |
| 27 | +# Define the GQE cost function |
| 28 | +def cost(sampledPoolOperations: list): |
| 29 | + """ |
| 30 | + Cost should take operator pool indices and |
| 31 | + return the associated cost. For the chemistry |
| 32 | + example, we'll take uccsd pool indices and return |
| 33 | + cudaq observe result |
| 34 | + """ |
| 35 | + # Convert the operator pool elements to cudaq.pauli_words |
| 36 | + asWords = [ |
| 37 | + cudaq.pauli_word(op.to_string(False)) for op in sampledPoolOperations |
| 38 | + ] |
| 39 | + |
| 40 | + # Get the pool coefficients as its own list |
| 41 | + operatorCoeffs = [op.get_coefficient().real for op in sampledPoolOperations] |
| 42 | + |
| 43 | + @cudaq.kernel |
| 44 | + def kernel(numQubits: int, coeffs: list[float], |
| 45 | + words: list[cudaq.pauli_word]): |
| 46 | + q = cudaq.qvector(numQubits) |
| 47 | + init(q) |
| 48 | + for i, word in enumerate(words): |
| 49 | + exp_pauli(coeffs[i], q, word) |
| 50 | + |
| 51 | + return cudaq.observe(kernel, molecule.hamiltonian, numQubits, |
| 52 | + operatorCoeffs, asWords).expectation() |
| 53 | + |
| 54 | + |
| 55 | +minE, optimPoolOps = cudaqlib.gqe(cost, pool, max_iters=10, energy_offset=184.) |
| 56 | +print(f'Ground Energy = {minE}') |
| 57 | +print('Ansatz Ops') |
| 58 | +for idx in optimPoolOps: |
| 59 | + print(pool[idx].get_coefficient().real, pool[idx].to_string(False)) |
0 commit comments