-
Notifications
You must be signed in to change notification settings - Fork 2
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
Conversation
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
Codecov ReportAll modified lines are covered by tests ✅
📢 Thoughts on this report? Let us know!. |
There was a problem hiding this 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😀
tests/test_hardware_efficient.py
Outdated
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in commit d21a1b4
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 |
for more information, see https://pre-commit.ci
Made the hardware-efficient ansatz with customizable parameterized gates, and added some unit tests.