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

Add n_qubits option to MultiformOperator compress function #209

Merged
merged 3 commits into from
Sep 12, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 9 additions & 5 deletions tangelo/toolboxes/operators/multiformoperator.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,14 @@ def remove_terms(self, indices):
# Update the terms attribute.
self.terms = integer_to_qubit_terms(self.integer, self.factors)

def _update(self):
"""Updates attributes according to self.terms."""
def _update(self, n_qubits=None):
"""Updates attributes according to self.terms.
Args:
n_qubits (int): Define n_qubits explicitly in update
"""

# Updating attributes.
self.n_qubits = count_qubits(self)
self.n_qubits = count_qubits(self) if n_qubits is None else n_qubits
self.factors = np.array([coeff for coeff in self.terms.values()])
self.integer = qubit_to_integer(self, self.n_qubits)
self.binary = integer_to_binary(self.integer)
Expand All @@ -214,13 +217,14 @@ def _update(self):
# Resetting the kernel attribute.
self.kernel = None

def compress(self, abs_tol=None):
def compress(self, abs_tol=None, n_qubits=None):
"""Overloads the QubitOperator.compress method. It adds an update for
the attributes.

Args:
abs_tol (float): Tolerance for the coefficients to be discarded or
not. By default, it is EQ_TOLERANCE=1e-8 (in openfermion).
n_qubits (int): Define n_qubits when compressing operator
"""

# If an update is done to EQ_TOLERANCE in openfermion, it will be
Expand All @@ -230,7 +234,7 @@ def compress(self, abs_tol=None):
else:
super(QubitOperator, self).compress(abs_tol)

self._update()
self._update(n_qubits)
ValentinS4t1qbit marked this conversation as resolved.
Show resolved Hide resolved

@staticmethod
def collapse(operator, factors):
Expand Down
186 changes: 186 additions & 0 deletions tangelo/toolboxes/operators/tests/data/H4_JW_spinupfirst.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
QubitOperator:
(-0.5864429644841693+0j) [] +
(-0.00815237463760797+0j) [X0 X1] +
(0.013296397151725512+0j) [X0 X1 X2 X3] +
(0.0004695311358986563+0j) [X0 X1 Y2 Y3] +
(0.003253529566556213+0j) [X0 X1 Z2] +
(0.006708972615038169+0j) [X0 X1 Z3] +
(0.03173407507759543+0j) [X0 X1 X4 X5] +
(0.03173407507759543+0j) [X0 X1 Y4 Y5] +
(0.013364563894405079+0j) [X0 X1 Z4] +
(-0.004603432481434373+0j) [X0 X1 Z5] +
(0.028904364464157548+0j) [X0 X1 X6 X7] +
(0.028904364464157548+0j) [X0 X1 Y6 Y7] +
(-0.0127735495930241+0j) [X0 X1 Z6] +
(0.01972455346200854+0j) [X0 X1 Z7] +
(0.012826866015826853+0j) [X0 Y1 Y2 X3] +
(0.02883609059721232+0j) [X0 Z1 X2 X4 Z5 X6] +
(-0.00880301904416748+0j) [X0 Z1 X2 X4 Z5 Z6 X7] +
(0.02883609059721232+0j) [X0 Z1 X2 Y4 Z5 Y6] +
(-0.00880301904416748+0j) [X0 Z1 X2 Y4 Z5 Z6 Y7] +
(0.016027079159580317+0j) [X0 Z1 X2 X5 X6] +
(0.028434833328258892+0j) [X0 Z1 X2 X5 Z6 X7] +
(0.016027079159580317+0j) [X0 Z1 X2 Y5 Y6] +
(0.028434833328258892+0j) [X0 Z1 X2 Y5 Z6 Y7] +
(-0.00880301904416748+0j) [X0 Z1 Z2 X3 X4 Z5 X6] +
(0.024655906633176298+0j) [X0 Z1 Z2 X3 X4 Z5 Z6 X7] +
(-0.00880301904416748+0j) [X0 Z1 Z2 X3 Y4 Z5 Y6] +
(0.024655906633176298+0j) [X0 Z1 Z2 X3 Y4 Z5 Z6 Y7] +
(0.015607967312432035+0j) [X0 Z1 Z2 X3 X5 X6] +
(-0.013015580846970371+0j) [X0 Z1 Z2 X3 X5 Z6 X7] +
(0.015607967312432035+0j) [X0 Z1 Z2 X3 Y5 Y6] +
(-0.013015580846970371+0j) [X0 Z1 Z2 X3 Y5 Z6 Y7] +
(0.012826866015826853+0j) [Y0 X1 X2 Y3] +
(-0.00815237463760797+0j) [Y0 Y1] +
(0.0004695311358986563+0j) [Y0 Y1 X2 X3] +
(0.013296397151725512+0j) [Y0 Y1 Y2 Y3] +
(0.003253529566556213+0j) [Y0 Y1 Z2] +
(0.006708972615038169+0j) [Y0 Y1 Z3] +
(0.03173407507759543+0j) [Y0 Y1 X4 X5] +
(0.03173407507759543+0j) [Y0 Y1 Y4 Y5] +
(0.013364563894405079+0j) [Y0 Y1 Z4] +
(-0.004603432481434373+0j) [Y0 Y1 Z5] +
(0.028904364464157548+0j) [Y0 Y1 X6 X7] +
(0.028904364464157548+0j) [Y0 Y1 Y6 Y7] +
(-0.0127735495930241+0j) [Y0 Y1 Z6] +
(0.01972455346200854+0j) [Y0 Y1 Z7] +
(0.02883609059721232+0j) [Y0 Z1 Y2 X4 Z5 X6] +
(-0.00880301904416748+0j) [Y0 Z1 Y2 X4 Z5 Z6 X7] +
(0.02883609059721232+0j) [Y0 Z1 Y2 Y4 Z5 Y6] +
(-0.00880301904416748+0j) [Y0 Z1 Y2 Y4 Z5 Z6 Y7] +
(0.016027079159580317+0j) [Y0 Z1 Y2 X5 X6] +
(0.028434833328258892+0j) [Y0 Z1 Y2 X5 Z6 X7] +
(0.016027079159580317+0j) [Y0 Z1 Y2 Y5 Y6] +
(0.028434833328258892+0j) [Y0 Z1 Y2 Y5 Z6 Y7] +
(-0.00880301904416748+0j) [Y0 Z1 Z2 Y3 X4 Z5 X6] +
(0.024655906633176298+0j) [Y0 Z1 Z2 Y3 X4 Z5 Z6 X7] +
(-0.00880301904416748+0j) [Y0 Z1 Z2 Y3 Y4 Z5 Y6] +
(0.024655906633176298+0j) [Y0 Z1 Z2 Y3 Y4 Z5 Z6 Y7] +
(0.015607967312432035+0j) [Y0 Z1 Z2 Y3 X5 X6] +
(-0.013015580846970371+0j) [Y0 Z1 Z2 Y3 X5 Z6 X7] +
(0.015607967312432035+0j) [Y0 Z1 Z2 Y3 Y5 Y6] +
(-0.013015580846970371+0j) [Y0 Z1 Z2 Y3 Y5 Z6 Y7] +
(0.19400432768831277+0j) [Z0] +
(0.08405878904131171+0j) [Z0 Z1] +
(0.003190168335865336+0j) [Z0 X2 X3] +
(0.003190168335865336+0j) [Z0 Y2 Y3] +
(0.08368205704693002+0j) [Z0 Z2] +
(0.10238128212053096+0j) [Z0 Z3] +
(0.013364563894405079+0j) [Z0 X4 X5] +
(0.013364563894405079+0j) [Z0 Y4 Y5] +
(0.1259950684949722+0j) [Z0 Z4] +
(0.11579286411890714+0j) [Z0 Z5] +
(0.011993187380032817+0j) [Z0 X6 X7] +
(0.011993187380032817+0j) [Z0 Y6 Y7] +
(0.11251814764414234+0j) [Z0 Z6] +
(0.12703718875370726+0j) [Z0 Z7] +
(0.016027079159580314+0j) [X1 X2 X4 Z5 X6] +
(0.015607967312432035+0j) [X1 X2 X4 Z5 Z6 X7] +
(0.016027079159580314+0j) [X1 X2 Y4 Z5 Y6] +
(0.015607967312432035+0j) [X1 X2 Y4 Z5 Z6 Y7] +
(0.028458362438533752+0j) [X1 X2 X5 X6] +
(0.012803045265112892+0j) [X1 X2 X5 Z6 X7] +
(0.028458362438533752+0j) [X1 X2 Y5 Y6] +
(0.012803045265112892+0j) [X1 X2 Y5 Z6 Y7] +
(0.028434833328258892+0j) [X1 Z2 X3 X4 Z5 X6] +
(-0.013015580846970371+0j) [X1 Z2 X3 X4 Z5 Z6 X7] +
(0.028434833328258892+0j) [X1 Z2 X3 Y4 Z5 Y6] +
(-0.013015580846970371+0j) [X1 Z2 X3 Y4 Z5 Z6 Y7] +
(0.012803045265112892+0j) [X1 Z2 X3 X5 X6] +
(0.03160023383081791+0j) [X1 Z2 X3 X5 Z6 X7] +
(0.012803045265112892+0j) [X1 Z2 X3 Y5 Y6] +
(0.03160023383081791+0j) [X1 Z2 X3 Y5 Z6 Y7] +
(0.016027079159580314+0j) [Y1 Y2 X4 Z5 X6] +
(0.015607967312432035+0j) [Y1 Y2 X4 Z5 Z6 X7] +
(0.016027079159580314+0j) [Y1 Y2 Y4 Z5 Y6] +
(0.015607967312432035+0j) [Y1 Y2 Y4 Z5 Z6 Y7] +
(0.028458362438533752+0j) [Y1 Y2 X5 X6] +
(0.012803045265112892+0j) [Y1 Y2 X5 Z6 X7] +
(0.028458362438533752+0j) [Y1 Y2 Y5 Y6] +
(0.012803045265112892+0j) [Y1 Y2 Y5 Z6 Y7] +
(0.028434833328258892+0j) [Y1 Z2 Y3 X4 Z5 X6] +
(-0.013015580846970371+0j) [Y1 Z2 Y3 X4 Z5 Z6 X7] +
(0.028434833328258892+0j) [Y1 Z2 Y3 Y4 Z5 Y6] +
(-0.013015580846970371+0j) [Y1 Z2 Y3 Y4 Z5 Z6 Y7] +
(0.012803045265112892+0j) [Y1 Z2 Y3 X5 X6] +
(0.03160023383081791+0j) [Y1 Z2 Y3 X5 Z6 X7] +
(0.012803045265112892+0j) [Y1 Z2 Y3 Y5 Y6] +
(0.03160023383081791+0j) [Y1 Z2 Y3 Y5 Z6 Y7] +
(0.009893101366460724+0j) [Z1] +
(0.006438934802050044+0j) [Z1 X2 X3] +
(0.006438934802050044+0j) [Z1 Y2 Y3] +
(0.0905863390015705+0j) [Z1 Z2] +
(0.08558472064472519+0j) [Z1 Z3] +
(-0.004603432481434373+0j) [Z1 X4 X5] +
(-0.004603432481434373+0j) [Z1 Y4 Y5] +
(0.11579286411890714+0j) [Z1 Z4] +
(0.1206597535200089+0j) [Z1 Z5] +
(-0.006364110463062851+0j) [Z1 X6 X7] +
(-0.006364110463062851+0j) [Z1 Y6 Y7] +
(0.11904470144010423+0j) [Z1 Z6] +
(0.1171849544755431+0j) [Z1 Z7] +
(0.00927325826564069+0j) [X2 X3] +
(0.028904364464157548+0j) [X2 X3 X4 X5] +
(0.028904364464157548+0j) [X2 X3 Y4 Y5] +
(0.011993187380032817+0j) [X2 X3 Z4] +
(-0.006364110463062851+0j) [X2 X3 Z5] +
(0.029384393827623683+0j) [X2 X3 X6 X7] +
(0.029384393827623683+0j) [X2 X3 Y6 Y7] +
(-0.012616232733648355+0j) [X2 X3 Z6] +
(0.01860115452289307+0j) [X2 X3 Z7] +
(0.00927325826564069+0j) [Y2 Y3] +
(0.028904364464157548+0j) [Y2 Y3 X4 X5] +
(0.028904364464157548+0j) [Y2 Y3 Y4 Y5] +
(0.011993187380032817+0j) [Y2 Y3 Z4] +
(-0.006364110463062851+0j) [Y2 Y3 Z5] +
(0.029384393827623683+0j) [Y2 Y3 X6 X7] +
(0.029384393827623683+0j) [Y2 Y3 Y6 Y7] +
(-0.012616232733648355+0j) [Y2 Y3 Z6] +
(0.01860115452289307+0j) [Y2 Y3 Z7] +
(0.008171090878118695+0j) [Z2] +
(0.08339319573976227+0j) [Z2 Z3] +
(-0.0127735495930241+0j) [Z2 X4 X5] +
(-0.0127735495930241+0j) [Z2 Y4 Y5] +
(0.11251814764414234+0j) [Z2 Z4] +
(0.11904470144010423+0j) [Z2 Z5] +
(-0.012616232733648355+0j) [Z2 X6 X7] +
(-0.012616232733648355+0j) [Z2 Y6 Y7] +
(0.12508828101233388+0j) [Z2 Z6] +
(0.11277758956738596+0j) [Z2 Z7] +
(-0.21882884081811788+0j) [Z3] +
(0.01972455346200854+0j) [Z3 X4 X5] +
(0.01972455346200854+0j) [Z3 Y4 Y5] +
(0.12703718875370726+0j) [Z3 Z4] +
(0.1171849544755431+0j) [Z3 Z5] +
(0.01860115452289307+0j) [Z3 X6 X7] +
(0.01860115452289307+0j) [Z3 Y6 Y7] +
(0.11277758956738596+0j) [Z3 Z6] +
(0.13832087291017262+0j) [Z3 Z7] +
(-0.008152374637607974+0j) [X4 X5] +
(0.013296397151725512+0j) [X4 X5 X6 X7] +
(0.0004695311358986563+0j) [X4 X5 Y6 Y7] +
(0.003253529566556213+0j) [X4 X5 Z6] +
(0.006708972615038169+0j) [X4 X5 Z7] +
(0.012826866015826853+0j) [X4 Y5 Y6 X7] +
(0.012826866015826853+0j) [Y4 X5 X6 Y7] +
(-0.008152374637607974+0j) [Y4 Y5] +
(0.0004695311358986563+0j) [Y4 Y5 X6 X7] +
(0.013296397151725512+0j) [Y4 Y5 Y6 Y7] +
(0.003253529566556213+0j) [Y4 Y5 Z6] +
(0.006708972615038169+0j) [Y4 Y5 Z7] +
(0.19400432768831277+0j) [Z4] +
(0.08405878904131171+0j) [Z4 Z5] +
(0.003190168335865336+0j) [Z4 X6 X7] +
(0.003190168335865336+0j) [Z4 Y6 Y7] +
(0.08368205704693002+0j) [Z4 Z6] +
(0.10238128212053096+0j) [Z4 Z7] +
(0.009893101366460676+0j) [Z5] +
(0.006438934802050044+0j) [Z5 X6 X7] +
(0.006438934802050044+0j) [Z5 Y6 Y7] +
(0.0905863390015705+0j) [Z5 Z6] +
(0.08558472064472519+0j) [Z5 Z7] +
(0.00927325826564069+0j) [X6 X7] +
(0.00927325826564069+0j) [Y6 Y7] +
(0.008171090878118643+0j) [Z6] +
(0.08339319573976227+0j) [Z6 Z7] +
(-0.21882884081811788+0j) [Z7]
13 changes: 13 additions & 0 deletions tangelo/toolboxes/operators/tests/test_taper_qubits.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ def test_z2taper_h2_jw_spinup_first(self):

self.assertAlmostEqual(e, e_taper, places=5)

def test_z2taper_h4_jw_spinup_first(self):
"""Test that a small qubit operator will be tapered properly when using tapering from a larger operator.
In this case, a 6 qubit operator from an 8 qubit operator tapering."""

qu_op = load_operator("H4_JW_spinupfirst.data", data_directory=pwd_this_test+"/data", plain_text=True)

tapering = QubitTapering(qu_op, 8, 4, 0, "JW", True)

try:
JamesB-1qbit marked this conversation as resolved.
Show resolved Hide resolved
tapering.z2_tapering(QubitOperator("X0 X1 X4 X5", 1), n_qubits=8)
except IndexError:
self.fail("z2_tapering raised IndexError unexpectedly")

def test_z2taper_h2_bk_occupied_first(self):
"""Test Z2 tapering of H2 BK up_then_down=False."""

Expand Down
4 changes: 2 additions & 2 deletions tangelo/toolboxes/operators/z2_tapering.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ def do_taper(operator, eigenvalues=eigenvalues):
op_matrix, factors = np.zeros(operator.n_qubits), np.array([0.0])
else:
product = operator * unitary
product.compress()
product.compress(n_qubits=operator.n_qubits)
product_reverse = unitary * product
product_reverse.compress()
product_reverse.compress(n_qubits=operator.n_qubits)
op_matrix, factors = product_reverse.integer, product_reverse.factors

if factors.max() == 0.0:
Expand Down