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

added vsqs ansatz #109

Merged
merged 10 commits into from
Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/continuous_integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
pip install qiskit==0.33.1 # Due to strange behaviour of noise model
pip install qulacs
pip install amazon-braket-sdk
pip install cirq==0.12.0
pip install cirq
pip install projectq
if: always()

Expand Down
25 changes: 25 additions & 0 deletions tangelo/algorithms/variational/tests/test_vqe_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,31 @@ def test_simulate_qcc_h2(self):
energy = vqe_solver.simulate()
self.assertAlmostEqual(energy, -1.137270, delta=1e-4)

def test_simulate_vsqs_h2(self):
"""Run VQE on H2 molecule, with vsqs ansatz, JW qubit mapping, exact simulator for both molecule input and
qubit_hamiltonian/hini/reference_state input
"""
vqe_options = {"molecule": mol_H2_sto3g, "ansatz": BuiltInAnsatze.VSQS, "qubit_mapping": "jw",
"verbose": False, "ansatz_options": {"intervals": 3, "time": 3}}
vqe_solver = VQESolver(vqe_options)
vqe_solver.build()

energy = vqe_solver.simulate()
self.assertAlmostEqual(energy, -1.137270, delta=1e-4)

qubit_hamiltonian = vqe_solver.qubit_hamiltonian
h_init = vqe_solver.ansatz.h_init
reference_state = vqe_solver.ansatz.prepare_reference_state()

vqe_options = {"molecule": None, "qubit_hamiltonian": qubit_hamiltonian, "ansatz": BuiltInAnsatze.VSQS, "qubit_mapping": "jw",
"ansatz_options": {"intervals": 3, "time": 3, "qubit_hamiltonian": qubit_hamiltonian,
"h_init": h_init, "reference_state": reference_state}}
vqe_solver = VQESolver(vqe_options)
vqe_solver.build()

energy = vqe_solver.simulate()
self.assertAlmostEqual(energy, -1.137270, delta=1e-4)

def test_simulate_h2_qiskit(self):
"""Run VQE on H2 molecule, with UCCSD ansatz, JW qubit mapping, initial
parameters, exact qiskit simulator.
Expand Down
16 changes: 9 additions & 7 deletions tangelo/algorithms/variational/vqe_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,7 @@
from tangelo.toolboxes.operators import count_qubits, FermionOperator, qubitop_to_qubitham
from tangelo.toolboxes.qubit_mappings.mapping_transform import fermion_to_qubit_mapping
from tangelo.toolboxes.ansatz_generator.ansatz import Ansatz
from tangelo.toolboxes.ansatz_generator.uccsd import UCCSD
from tangelo.toolboxes.ansatz_generator.rucc import RUCC
from tangelo.toolboxes.ansatz_generator.hea import HEA
from tangelo.toolboxes.ansatz_generator.upccgsd import UpCCGSD
from tangelo.toolboxes.ansatz_generator.qmf import QMF
from tangelo.toolboxes.ansatz_generator.qcc import QCC
from tangelo.toolboxes.ansatz_generator.variational_circuit import VariationalCircuitAnsatz
from tangelo.toolboxes.ansatz_generator import UCCSD, RUCC, HEA, UpCCGSD, QMF, QCC, VSQS, VariationalCircuitAnsatz
from tangelo.toolboxes.ansatz_generator.penalty_terms import combined_penalty
from tangelo.toolboxes.post_processing.bootstrapping import get_resampled_frequencies
from tangelo.toolboxes.ansatz_generator.fermionic_operators import number_operator, spinz_operator, spin2_operator
Expand All @@ -50,6 +44,7 @@ class BuiltInAnsatze(Enum):
UpCCGSD = 4
QMF = 5
QCC = 6
VSQS = 7


class VQESolver:
Expand Down Expand Up @@ -186,12 +181,19 @@ def build(self):
self.ansatz = QMF(self.molecule, self.qubit_mapping, self.up_then_down, **self.ansatz_options)
elif self.ansatz == BuiltInAnsatze.QCC:
self.ansatz = QCC(self.molecule, self.qubit_mapping, self.up_then_down, **self.ansatz_options)
elif self.ansatz == BuiltInAnsatze.VSQS:
self.ansatz = VSQS(self.molecule, self.qubit_mapping, self.up_then_down, **self.ansatz_options)
JamesB-1qbit marked this conversation as resolved.
Show resolved Hide resolved
else:
raise ValueError(f"Unsupported ansatz. Built-in ansatze:\n\t{self.builtin_ansatze}")
elif not isinstance(self.ansatz, Ansatz):
raise TypeError(f"Invalid ansatz dataype. Expecting instance of Ansatz class, or one of built-in options:\n\t{self.builtin_ansatze}")

# Building with a qubit Hamiltonian.
elif self.ansatz in [BuiltInAnsatze.HEA, BuiltInAnsatze.VSQS]:
if self.ansatz == BuiltInAnsatze.HEA:
self.ansatz = HEA(self.molecule, self.qubit_mapping, self.up_then_down, **self.ansatz_options)
elif self.ansatz == BuiltInAnsatze.VSQS:
self.ansatz = VSQS(self.molecule, self.qubit_mapping, self.up_then_down, **self.ansatz_options)
elif not isinstance(self.ansatz, Ansatz):
raise TypeError(f"Invalid ansatz dataype. Expecting a custom Ansatz (Ansatz class).")

Expand Down
10 changes: 10 additions & 0 deletions tangelo/toolboxes/ansatz_generator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,13 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from .vsqs import VSQS
from .qcc import QCC
from .qmf import QMF
from .uccsd import UCCSD
from .adapt_ansatz import ADAPTAnsatz
from .rucc import RUCC
from .upccgsd import UpCCGSD
from .hea import HEA
from .variational_circuit import VariationalCircuitAnsatz
14 changes: 11 additions & 3 deletions tangelo/toolboxes/ansatz_generator/ansatz_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def exp_pauliword_to_gates(pauli_word, coef, variational=True, control=None):


def get_exponentiated_qubit_operator_circuit(qubit_op, time=1., variational=False, trotter_order=1, control=None,
return_phase=False):
return_phase=False, pauli_order=None):
"""Generate the exponentiation of a qubit operator in first- or second-order Trotterized form.
The algorithm is described in Whitfield 2010 https://arxiv.org/pdf/1001.3855.pdf

Expand All @@ -94,12 +94,20 @@ def get_exponentiated_qubit_operator_circuit(qubit_op, time=1., variational=Fals
variational (bool) : Whether the coefficients are variational
trotter_order (int): order of trotter approximation, only 1 or 2 are supported.
return_phase (bool): Return the global-phase generated
pauli_order (list): The desired pauli_word order for trotterization defined as a list of (pauli_word, coeff)
elements which have matching dictionary elements pauli_word: coeff in QubitOperator terms.items().
The coeff in pauli_order is used to generate the exponential.

Returns:
Circuit: circuit corresponding to exponentiation of qubit operator
phase : The global phase of the time evolution if return_phase=True else not included
"""
pauli_words = list(qubit_op.terms.items())
if pauli_order is None:
pauli_words = list(qubit_op.terms.items())
elif isinstance(pauli_order, list):
pauli_words = pauli_order.copy()
else:
raise ValueError("ordered terms must be a list with elements (keys, values) of qubit_op.terms.items()")

if trotter_order > 2:
raise ValueError(f"Trotter order of >2 is not supported currently in Tangelo.")
Expand All @@ -118,7 +126,7 @@ def get_exponentiated_qubit_operator_circuit(qubit_op, time=1., variational=Fals
phase = 1.
exp_pauli_word_gates = list()
for i in range(trotter_order):
if i == 0:
if i == 1:
alexfleury-sb marked this conversation as resolved.
Show resolved Hide resolved
pauli_words.reverse()
for pauli_word, coef in pauli_words:
if pauli_word: # identity terms do not contribute to evolution outside of a phase
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
QubitOperator:
(-3.201203136341336+0j) [] +
(0.018922220448748025+0j) [X0 X1] +
(0.018922220448748025+0j) [Y0 Y1] +
(0.6887270305253504+0j) [Z0] +
(0.37691668685924784+0j) [Z1] +
(-0.02281117974146273+0j) [X2 X3] +
(-0.02281117974146273+0j) [Y2 Y3] +
(0.3753113309246301+0j) [Z2] +
(0.15964651986143952+0j) [Z3] +
(0.018922220448748025+0j) [X4 X5] +
(0.018922220448748025+0j) [Y4 Y5] +
(0.6887270305253504+0j) [Z4] +
(0.37691668685924784+0j) [Z5] +
(-0.02281117974146273+0j) [X6 X7] +
(-0.02281117974146273+0j) [Y6 Y7] +
(0.3753113309246301+0j) [Z6] +
(0.15964651986143952+0j) [Z7]
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
QubitOperator:
(-0.845088084959612+0j) [] +
(-0.0020776113200212075+0j) [X0 X1] +
(-0.011761015389060145+0j) [X0 X1 X2 X3] +
(-0.0003365342989052316+0j) [X0 X1 Y2 Y3] +
(0.002844217831601514+0j) [X0 X1 Z2] +
(0.005067140284950715+0j) [X0 X1 Z3] +
(0.02660445410908035+0j) [X0 X1 X4 X5] +
(0.02660445410908035+0j) [X0 X1 Y4 Y5] +
(0.009461086978084416+0j) [X0 X1 Z4] +
(-0.002045316086697597+0j) [X0 X1 Z5] +
(-0.024395190370696845+0j) [X0 X1 X6 X7] +
(-0.024395190370696845+0j) [X0 X1 Y6 Y7] +
(-0.007801932778402895+0j) [X0 X1 Z6] +
(0.013474635539233077+0j) [X0 X1 Z7] +
(-0.011424481090154913+0j) [X0 Y1 Y2 X3] +
(0.024934767638066777+0j) [X0 Z1 X2 X4 Z5 X6] +
(0.006238164536781785+0j) [X0 Z1 X2 X4 Z5 Z6 X7] +
(0.024934767638066777+0j) [X0 Z1 X2 Y4 Z5 Y6] +
(0.006238164536781785+0j) [X0 Z1 X2 Y4 Z5 Z6 Y7] +
(0.01064615061000441+0j) [X0 Z1 X2 X5 X6] +
(-0.02405865607179162+0j) [X0 Z1 X2 X5 Z6 X7] +
(0.01064615061000441+0j) [X0 Z1 X2 Y5 Y6] +
(-0.02405865607179162+0j) [X0 Z1 X2 Y5 Z6 Y7] +
(0.006238164536781785+0j) [X0 Z1 Z2 X3 X4 Z5 X6] +
(0.017676042848046+0j) [X0 Z1 Z2 X3 X4 Z5 Z6 X7] +
(0.006238164536781785+0j) [X0 Z1 Z2 X3 Y4 Z5 Y6] +
(0.017676042848046+0j) [X0 Z1 Z2 X3 Y4 Z5 Z6 Y7] +
(-0.012634174981636702+0j) [X0 Z1 Z2 X3 X5 X6] +
(-0.008407495254282365+0j) [X0 Z1 Z2 X3 X5 Z6 X7] +
(-0.012634174981636702+0j) [X0 Z1 Z2 X3 Y5 Y6] +
(-0.008407495254282365+0j) [X0 Z1 Z2 X3 Y5 Z6 Y7] +
(-0.011424481090154913+0j) [Y0 X1 X2 Y3] +
(-0.0020776113200212075+0j) [Y0 Y1] +
(-0.0003365342989052316+0j) [Y0 Y1 X2 X3] +
(-0.011761015389060145+0j) [Y0 Y1 Y2 Y3] +
(0.002844217831601514+0j) [Y0 Y1 Z2] +
(0.005067140284950715+0j) [Y0 Y1 Z3] +
(0.02660445410908035+0j) [Y0 Y1 X4 X5] +
(0.02660445410908035+0j) [Y0 Y1 Y4 Y5] +
(0.009461086978084416+0j) [Y0 Y1 Z4] +
(-0.002045316086697597+0j) [Y0 Y1 Z5] +
(-0.024395190370696845+0j) [Y0 Y1 X6 X7] +
(-0.024395190370696845+0j) [Y0 Y1 Y6 Y7] +
(-0.007801932778402895+0j) [Y0 Y1 Z6] +
(0.013474635539233077+0j) [Y0 Y1 Z7] +
(0.024934767638066777+0j) [Y0 Z1 Y2 X4 Z5 X6] +
(0.006238164536781785+0j) [Y0 Z1 Y2 X4 Z5 Z6 X7] +
(0.024934767638066777+0j) [Y0 Z1 Y2 Y4 Z5 Y6] +
(0.006238164536781785+0j) [Y0 Z1 Y2 Y4 Z5 Z6 Y7] +
(0.01064615061000441+0j) [Y0 Z1 Y2 X5 X6] +
(-0.02405865607179162+0j) [Y0 Z1 Y2 X5 Z6 X7] +
(0.01064615061000441+0j) [Y0 Z1 Y2 Y5 Y6] +
(-0.02405865607179162+0j) [Y0 Z1 Y2 Y5 Z6 Y7] +
(0.006238164536781785+0j) [Y0 Z1 Z2 Y3 X4 Z5 X6] +
(0.017676042848046+0j) [Y0 Z1 Z2 Y3 X4 Z5 Z6 X7] +
(0.006238164536781785+0j) [Y0 Z1 Z2 Y3 Y4 Z5 Y6] +
(0.017676042848046+0j) [Y0 Z1 Z2 Y3 Y4 Z5 Z6 Y7] +
(-0.012634174981636702+0j) [Y0 Z1 Z2 Y3 X5 X6] +
(-0.008407495254282365+0j) [Y0 Z1 Z2 Y3 X5 Z6 X7] +
(-0.012634174981636702+0j) [Y0 Z1 Z2 Y3 Y5 Y6] +
(-0.008407495254282365+0j) [Y0 Z1 Z2 Y3 Y5 Z6 Y7] +
(0.23477912416223978+0j) [Z0] +
(0.07755867751288839+0j) [Z0 Z1] +
(-0.0025837417313018998+0j) [Z0 X2 X3] +
(-0.0025837417313018998+0j) [Z0 Y2 Y3] +
(0.07733247653566544+0j) [Z0 Z2] +
(0.09333330628587891+0j) [Z0 Z3] +
(0.009461086978084416+0j) [Z0 X4 X5] +
(0.009461086978084416+0j) [Z0 Y4 Y5] +
(0.11171627890094822+0j) [Z0 Z4] +
(0.10416313162196875+0j) [Z0 Z5] +
(-0.008821906268083685+0j) [Z0 X6 X7] +
(-0.008821906268083685+0j) [Z0 Y6 Y7] +
(0.10226724417373223+0j) [Z0 Z6] +
(0.1110093491339249+0j) [Z0 Z7] +
(0.010646150610004408+0j) [X1 X2 X4 Z5 X6] +
(-0.012634174981636702+0j) [X1 X2 X4 Z5 Z6 X7] +
(0.010646150610004408+0j) [X1 X2 Y4 Z5 Y6] +
(-0.012634174981636702+0j) [X1 X2 Y4 Z5 Z6 Y7] +
(0.01934883030685017+0j) [X1 X2 X5 X6] +
(-0.008901638567159004+0j) [X1 X2 X5 Z6 X7] +
(0.01934883030685017+0j) [X1 X2 Y5 Y6] +
(-0.008901638567159004+0j) [X1 X2 Y5 Z6 Y7] +
(-0.02405865607179162+0j) [X1 Z2 X3 X4 Z5 X6] +
(-0.008407495254282365+0j) [X1 Z2 X3 X4 Z5 Z6 X7] +
(-0.02405865607179162+0j) [X1 Z2 X3 Y4 Z5 Y6] +
(-0.008407495254282365+0j) [X1 Z2 X3 Y4 Z5 Z6 Y7] +
(-0.008901638567159004+0j) [X1 Z2 X3 X5 X6] +
(0.02661467635818076+0j) [X1 Z2 X3 X5 Z6 X7] +
(-0.008901638567159004+0j) [X1 Z2 X3 Y5 Y6] +
(0.02661467635818076+0j) [X1 Z2 X3 Y5 Z6 Y7] +
(0.010646150610004408+0j) [Y1 Y2 X4 Z5 X6] +
(-0.012634174981636702+0j) [Y1 Y2 X4 Z5 Z6 X7] +
(0.010646150610004408+0j) [Y1 Y2 Y4 Z5 Y6] +
(-0.012634174981636702+0j) [Y1 Y2 Y4 Z5 Z6 Y7] +
(0.01934883030685017+0j) [Y1 Y2 X5 X6] +
(-0.008901638567159004+0j) [Y1 Y2 X5 Z6 X7] +
(0.01934883030685017+0j) [Y1 Y2 Y5 Y6] +
(-0.008901638567159004+0j) [Y1 Y2 Y5 Z6 Y7] +
(-0.02405865607179162+0j) [Y1 Z2 Y3 X4 Z5 X6] +
(-0.008407495254282365+0j) [Y1 Z2 Y3 X4 Z5 Z6 X7] +
(-0.02405865607179162+0j) [Y1 Z2 Y3 Y4 Z5 Y6] +
(-0.008407495254282365+0j) [Y1 Z2 Y3 Y4 Z5 Z6 Y7] +
(-0.008901638567159004+0j) [Y1 Z2 Y3 X5 X6] +
(0.02661467635818076+0j) [Y1 Z2 Y3 X5 Z6 X7] +
(-0.008901638567159004+0j) [Y1 Z2 Y3 Y5 Y6] +
(0.02661467635818076+0j) [Y1 Z2 Y3 Y5 Z6 Y7] +
(0.07873792608304384+0j) [Z1] +
(-0.00523830590529105+0j) [Z1 X2 X3] +
(-0.00523830590529105+0j) [Z1 Y2 Y3] +
(0.08417436673290517+0j) [Z1 Z2] +
(0.07928157442541509+0j) [Z1 Z3] +
(-0.002045316086697597+0j) [Z1 X4 X5] +
(-0.002045316086697597+0j) [Z1 Y4 Y5] +
(0.10416313162196875+0j) [Z1 Z4] +
(0.10702518092938991+0j) [Z1 Z5] +
(0.0036633326618679546+0j) [Z1 X6 X7] +
(0.0036633326618679546+0j) [Z1 Y6 Y7] +
(0.10352319703975532+0j) [Z1 Z6] +
(0.10589625078359582+0j) [Z1 Z7] +
(-0.004483252597706383+0j) [X2 X3] +
(-0.024395190370696845+0j) [X2 X3 X4 X5] +
(-0.024395190370696845+0j) [X2 X3 Y4 Y5] +
(-0.008821906268083685+0j) [X2 X3 Z4] +
(0.0036633326618679546+0j) [X2 X3 Z5] +
(0.025689949571830042+0j) [X2 X3 X6 X7] +
(0.025689949571830042+0j) [X2 X3 Y6 Y7] +
(0.007649625311503733+0j) [X2 X3 Z6] +
(-0.012996931212451403+0j) [X2 X3 Z7] +
(-0.004483252597706383+0j) [Y2 Y3] +
(-0.024395190370696845+0j) [Y2 Y3 X4 X5] +
(-0.024395190370696845+0j) [Y2 Y3 Y4 Y5] +
(-0.008821906268083685+0j) [Y2 Y3 Z4] +
(0.0036633326618679546+0j) [Y2 Y3 Z5] +
(0.025689949571830042+0j) [Y2 Y3 X6 X7] +
(0.025689949571830042+0j) [Y2 Y3 Y6 Y7] +
(0.007649625311503733+0j) [Y2 Y3 Z6] +
(-0.012996931212451403+0j) [Y2 Y3 Z7] +
(0.07666866090982398+0j) [Z2] +
(0.07784794028451397+0j) [Z2 Z3] +
(-0.007801932778402895+0j) [Z2 X4 X5] +
(-0.007801932778402895+0j) [Z2 Y4 Y5] +
(0.10226724417373223+0j) [Z2 Z4] +
(0.10352319703975532+0j) [Z2 Z5] +
(0.007649625311503733+0j) [Z2 X6 X7] +
(0.007649625311503733+0j) [Z2 Y6 Y7] +
(0.1091589968106852+0j) [Z2 Z6] +
(0.10353788985634402+0j) [Z2 Z7] +
(-0.12227609382406271+0j) [Z3] +
(0.013474635539233077+0j) [Z3 X4 X5] +
(0.013474635539233077+0j) [Z3 Y4 Y5] +
(0.1110093491339249+0j) [Z3 Z4] +
(0.10589625078359582+0j) [Z3 Z5] +
(-0.012996931212451403+0j) [Z3 X6 X7] +
(-0.012996931212451403+0j) [Z3 Y6 Y7] +
(0.10353788985634402+0j) [Z3 Z6] +
(0.11970161375543707+0j) [Z3 Z7] +
(-0.0020776113200212066+0j) [X4 X5] +
(-0.011761015389060145+0j) [X4 X5 X6 X7] +
(-0.0003365342989052316+0j) [X4 X5 Y6 Y7] +
(0.002844217831601514+0j) [X4 X5 Z6] +
(0.005067140284950715+0j) [X4 X5 Z7] +
(-0.011424481090154913+0j) [X4 Y5 Y6 X7] +
(-0.011424481090154913+0j) [Y4 X5 X6 Y7] +
(-0.0020776113200212066+0j) [Y4 Y5] +
(-0.0003365342989052316+0j) [Y4 Y5 X6 X7] +
(-0.011761015389060145+0j) [Y4 Y5 Y6 Y7] +
(0.002844217831601514+0j) [Y4 Y5 Z6] +
(0.005067140284950715+0j) [Y4 Y5 Z7] +
(0.23477912416223978+0j) [Z4] +
(0.07755867751288839+0j) [Z4 Z5] +
(-0.0025837417313018998+0j) [Z4 X6 X7] +
(-0.0025837417313018998+0j) [Z4 Y6 Y7] +
(0.07733247653566544+0j) [Z4 Z6] +
(0.09333330628587891+0j) [Z4 Z7] +
(0.07873792608304378+0j) [Z5] +
(-0.00523830590529105+0j) [Z5 X6 X7] +
(-0.00523830590529105+0j) [Z5 Y6 Y7] +
(0.08417436673290517+0j) [Z5 Z6] +
(0.07928157442541509+0j) [Z5 Z7] +
(-0.004483252597706375+0j) [X6 X7] +
(-0.004483252597706375+0j) [Y6 Y7] +
(0.07666866090982404+0j) [Z6] +
(0.07784794028451397+0j) [Z6 Z7] +
(-0.12227609382406274+0j) [Z7]
Loading