Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customizable hardware-efficient ansatz #43

Merged
merged 9 commits into from
Oct 4, 2023
Merged

Customizable hardware-efficient ansatz #43

merged 9 commits into from
Oct 4, 2023

Conversation

damarkian
Copy link
Contributor

Made the hardware-efficient ansatz with customizable parameterized gates, and added some unit tests.

@damarkian damarkian requested review from chmwzc and TL231 September 12, 2023 06:16
@codecov
Copy link

codecov bot commented Sep 12, 2023

Codecov Report

All modified lines are covered by tests ✅

Files Coverage Δ
src/qibochem/ansatz/hardware_efficient.py 100.00% <100.00%> (+100.00%) ⬆️

📢 Thoughts on this report? Let us know!.

Copy link
Contributor

@chmwzc chmwzc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still needs documentation, but since I'm already working on the API documentation, I can do that after this is done😀

src/qibochem/ansatz/hardware_efficient.py Outdated Show resolved Hide resolved
src/qibochem/ansatz/hardware_efficient.py Show resolved Hide resolved
vqe_object = minimize(test_vqe_hea_ansatz_cost, theta, args=(qc, mol_sym_ham), method="Powell")

vqe_hf_energy = vqe_object.fun
assert mol_classical_hf_energy == pytest.approx(vqe_hf_energy, 0.01)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wondering, rather than testing against the HF energy again, would it be better for the VQE test to get the exact ground state energy instead? We can just test against a hardcoded value ( -1.13711707 ), no need to solve for it every time.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC this is at most what the HEA gives you, as we are only working within the space of unitaries that optimize each spin orbital. A systematic scan of all theta's within the parameter space should show the minimum being the HF energy.

I don't think it will give you the FCI value, as it is optimized for hardware implementation and will not be as expressive as a chemically inspired ansatz such as UCCSD, which includes doubles excitations that get JW'd or BK'd and go into the ansatz thereafter.

One thing that can be done, is to not complete the SCF at the pyscf phase, and use this vqe process to obtain the correct parameters that give you the HF energy at SCF.

What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do remember getting the FCI value when we did the DSO workshop last year, so I went back to look at it and did some testing to compare my code there to your code here.

So, I can't get the FCI value when coupled_gate is set to be a CZ gate, but can get it using CNOT gates instead. Try the following:

# Cost function
def test_vqe_hea_ansatz_cost(parameters, circuit, hamiltonian):
    circuit.set_parameters(parameters)
    return expectation(circuit, hamiltonian)

# Build Molecule
mol = Molecule([("H", (0.0, 0.0, 0.0)), ("H", (0.0, 0.0, 0.75))])
mol.run_pyscf()
mol_classical_hf_energy = mol.e_hf
mol_sym_ham = mol.hamiltonian("s")

nlayers = 2
nqubits = mol.nso

# Build HEA circuit using CZ gates
qc = models.Circuit(nqubits)
hea_cz_ansatz = hardware_efficient.hea(nlayers, nqubits)
qc.add(hea_cz_ansatz )

# Build HEA circuit using CNOT gates
qc2 = models.Circuit(nqubits)
hea_cnot_ansatz = hardware_efficient.hea(nlayers, nqubits, coupling_gates="CNOT")
qc2.add(hea_cnot_ansatz )

print("CZ coupling gates result:")
for _ in range(10):
    ntheta = len(qc.get_parameters())
    theta = np.random.rand(ntheta)
    vqe_object = minimize(test_vqe_hea_ansatz_cost, theta, args=(qc, mol_sym_ham), method="Powell")
    print(vqe_object.fun)
print()

print("CNOT coupling gates result:")
for _ in range(10):
    ntheta = len(qc2.get_parameters())
    theta = np.random.rand(ntheta)
    vqe_object = minimize(test_vqe_hea_ansatz_cost, theta, args=(qc2, mol_sym_ham), method="Powell")
    print(vqe_object.fun)

My CZ results are all about the HF value, while the CNOT results are either totally crap (-0.54...) or the FCI value.
Whether or not X gates are added before the HEA doesn't seem to affect the results much too.

Copy link
Contributor Author

@damarkian damarkian Sep 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed.

It seems that for CZ coupling gates, we are limiting our scope of unitaries somewhat.

I tried your CNOT method with 3 layers and I got the FCI solution for all 10 runs.

Maybe we should omit the vqe test function for now? Or give it the 3 (or more) layer test, with a non-random starting point so that the result would be a deterministic one?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I would prefer to keep the VQE test and just use fixed initial values of theta for the optimization. Don't think we want the test to be too big, so just 3 layers would be good.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in commit d21a1b4

@damarkian
Copy link
Contributor Author

Still needs documentation, but since I'm already working on the API documentation, I can do that after this is done😀

Ok, I hope you are working off my starting point, the one with the Molecule class already added? Then we just add docstrings to each class & function, and sphinx will just self-populate. No need to start from zero :)

@chmwzc
Copy link
Contributor

chmwzc commented Sep 13, 2023

Still needs documentation, but since I'm already working on the API documentation, I can do that after this is done😀

Ok, I hope you are working off my starting point, the one with the Molecule class already added? Then we just add docstrings to each class & function, and sphinx will just self-populate. No need to start from zero :)

Haha, that was one reason why I suggested merging that branch into main in the last meeting😄

@chmwzc chmwzc merged commit 009cb55 into main Oct 4, 2023
6 checks passed
@chmwzc chmwzc deleted the hea_general branch October 4, 2023 02:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants