From d9710c6b2127586ec538df4cbee44fd0baef6af2 Mon Sep 17 00:00:00 2001 From: Kuang-Yu Samuel Chang Date: Sat, 28 Jun 2025 21:44:01 +0200 Subject: [PATCH 1/8] Update `orbital_energies` to `diagonalizing_bogoliubov_transform` --- docs/tutorials/intro_to_openfermion.ipynb | 2 +- docs/tutorials/intro_workshop_exercises.ipynb | 11 +++++------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/docs/tutorials/intro_to_openfermion.ipynb b/docs/tutorials/intro_to_openfermion.ipynb index 9e4e794f6..4287c8916 100644 --- a/docs/tutorials/intro_to_openfermion.ipynb +++ b/docs/tutorials/intro_to_openfermion.ipynb @@ -840,7 +840,7 @@ }, "outputs": [], "source": [ - "orbital_energies, constant = quadratic_hamiltonian.orbital_energies()\n", + "orbital_energies, _, constant = quadratic_hamiltonian.diagonalizing_bogoliubov_transform()\n", "print(orbital_energies)\n", "print()\n", "print(constant)" diff --git a/docs/tutorials/intro_workshop_exercises.ipynb b/docs/tutorials/intro_workshop_exercises.ipynb index ba985bb9e..ee79c74d3 100644 --- a/docs/tutorials/intro_workshop_exercises.ipynb +++ b/docs/tutorials/intro_workshop_exercises.ipynb @@ -902,7 +902,7 @@ ")\n", "\n", "# Apply the circuit to the initial state\n", - "result = circuit.final_state_vector(initial_state)\n", + "result = circuit.final_state_vector(initial_state=initial_state)\n", "\n", "# Compute the fidelity with the final state from exact evolution\n", "fidelity = abs(np.dot(exact_state, result.conj())) ** 2\n", @@ -2050,7 +2050,7 @@ } ], "source": [ - "orbital_energies, constant = quad_ham.orbital_energies()\n", + "orbital_energies, _, constant = quad_ham.diagonalizing_bogoliubov_transform()\n", "\n", "print(orbital_energies)\n", "print(constant)" @@ -2168,7 +2168,7 @@ "# ---------------------------------------------\n", "\n", "# Apply the circuit to the initial state\n", - "result = circuit.final_state_vector(initial_state)\n", + "result = circuit.final_state_vector(initial_state=initial_state)\n", "\n", "# Compute the fidelity with the correct final state\n", "fidelity = abs(np.dot(final_state, result.conj())) ** 2\n", @@ -2209,8 +2209,7 @@ "# You should define the `circuit` variable here\n", "# ---------------------------------------------\n", "def exponentiate_quad_ham(qubits, quad_ham):\n", - " _, basis_change_matrix, _ = quad_ham.diagonalizing_bogoliubov_transform()\n", - " orbital_energies, _ = quad_ham.orbital_energies()\n", + " orbital_energies, basis_change_matrix, _ = quad_ham.diagonalizing_bogoliubov_transform()\n", "\n", " yield cirq.inverse(of.bogoliubov_transform(qubits, basis_change_matrix))\n", " for i in range(len(qubits)):\n", @@ -2222,7 +2221,7 @@ "# ---------------------------------------------\n", "\n", "# Apply the circuit to the initial state\n", - "result = circuit.final_state_vector(initial_state)\n", + "result = circuit.final_state_vector(initial_state=initial_state)\n", "\n", "# Compute the fidelity with the correct final state\n", "fidelity = abs(np.dot(final_state, result.conj())) ** 2\n", From 326f9ddf3b780cbef2e58cf9a3045d3f8db4283e Mon Sep 17 00:00:00 2001 From: Kuang-Yu Samuel Chang Date: Sat, 28 Jun 2025 22:14:10 +0200 Subject: [PATCH 2/8] Raise error when argument to `linearize_term` is non-physical --- src/openfermion/measurements/equality_constraint_projection.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/openfermion/measurements/equality_constraint_projection.py b/src/openfermion/measurements/equality_constraint_projection.py index 2ba9d6e6c..e3c699297 100644 --- a/src/openfermion/measurements/equality_constraint_projection.py +++ b/src/openfermion/measurements/equality_constraint_projection.py @@ -51,6 +51,7 @@ def linearize_term(term, n_orbitals): r = term[2][0] s = term[3][0] return 1 + n_orbitals**2 + p + q * n_orbitals + r * n_orbitals**2 + s * n_orbitals**3 + raise ValueError('Expect one-body (2 terms) or two-body (4 terms) operator but got {} terms. '.format(len(term))) def unlinearize_term(index, n_orbitals): From 07e2900408b522007e7eba97a260edce988bfb35 Mon Sep 17 00:00:00 2001 From: Kuang-Yu Samuel Chang Date: Mon, 30 Jun 2025 19:28:06 +0200 Subject: [PATCH 3/8] Update equality_constraint_projection.py FIx format --- .../measurements/equality_constraint_projection.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/openfermion/measurements/equality_constraint_projection.py b/src/openfermion/measurements/equality_constraint_projection.py index e3c699297..184a7ab49 100644 --- a/src/openfermion/measurements/equality_constraint_projection.py +++ b/src/openfermion/measurements/equality_constraint_projection.py @@ -51,7 +51,11 @@ def linearize_term(term, n_orbitals): r = term[2][0] s = term[3][0] return 1 + n_orbitals**2 + p + q * n_orbitals + r * n_orbitals**2 + s * n_orbitals**3 - raise ValueError('Expect one-body (2 terms) or two-body (4 terms) operator but got {} terms. '.format(len(term))) + raise ValueError( + 'Expect one-body (2 terms) or two-body (4 terms) operator but got {} terms. '.format( + len(term) + ) + ) def unlinearize_term(index, n_orbitals): From 2908c7499ad0a32e1e473f8a0483fd2926273427 Mon Sep 17 00:00:00 2001 From: Kuang-Yu Samuel Chang Date: Wed, 2 Jul 2025 02:12:45 +0200 Subject: [PATCH 4/8] Update equality_constraint_projection_test.py Add a test cases to catch value error --- .../measurements/equality_constraint_projection_test.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/openfermion/measurements/equality_constraint_projection_test.py b/src/openfermion/measurements/equality_constraint_projection_test.py index d59f6a9f0..952b077e1 100644 --- a/src/openfermion/measurements/equality_constraint_projection_test.py +++ b/src/openfermion/measurements/equality_constraint_projection_test.py @@ -59,6 +59,12 @@ def test_linearize_term(self): self.assertFalse(index in past_terms) past_terms.add(index) + def test_error_with_non_physical_term(self): + non_physical_operator = openfermion.FermionOperator((0, 1)) + with self.assertRaises(ValueError): + linearize_term(non_physical_operator, self.n_orbitals) + + def test_unlinearize_term_consistency(self): for term, _ in self.fermion_hamiltonian.terms.items(): index = linearize_term(term, self.n_orbitals) From 46f6108875c78ba8d92c89be17370ea196a71bc4 Mon Sep 17 00:00:00 2001 From: Kuang-Yu Samuel Chang Date: Wed, 2 Jul 2025 02:13:18 +0200 Subject: [PATCH 5/8] Update equality_constraint_projection_test.py --- .../measurements/equality_constraint_projection_test.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/openfermion/measurements/equality_constraint_projection_test.py b/src/openfermion/measurements/equality_constraint_projection_test.py index 952b077e1..abd00d40d 100644 --- a/src/openfermion/measurements/equality_constraint_projection_test.py +++ b/src/openfermion/measurements/equality_constraint_projection_test.py @@ -63,7 +63,6 @@ def test_error_with_non_physical_term(self): non_physical_operator = openfermion.FermionOperator((0, 1)) with self.assertRaises(ValueError): linearize_term(non_physical_operator, self.n_orbitals) - def test_unlinearize_term_consistency(self): for term, _ in self.fermion_hamiltonian.terms.items(): From 672e046bc3d7f2f3b8096df990b633c66607bc8d Mon Sep 17 00:00:00 2001 From: Kuang-Yu Samuel Chang Date: Wed, 2 Jul 2025 02:19:49 +0200 Subject: [PATCH 6/8] Update equality_constraint_projection_test.py --- .../measurements/equality_constraint_projection_test.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/openfermion/measurements/equality_constraint_projection_test.py b/src/openfermion/measurements/equality_constraint_projection_test.py index abd00d40d..e09856e6e 100644 --- a/src/openfermion/measurements/equality_constraint_projection_test.py +++ b/src/openfermion/measurements/equality_constraint_projection_test.py @@ -16,6 +16,7 @@ from openfermion.chem import MolecularData from openfermion.config import DATA_DIRECTORY +from openfermion.ops.operators import FermionOperator from openfermion.transforms.opconversions import get_fermion_operator from openfermion.linalg import ( get_sparse_operator, @@ -60,7 +61,7 @@ def test_linearize_term(self): past_terms.add(index) def test_error_with_non_physical_term(self): - non_physical_operator = openfermion.FermionOperator((0, 1)) + non_physical_operator = FermionOperator((0, 1)) with self.assertRaises(ValueError): linearize_term(non_physical_operator, self.n_orbitals) From bc96fe6fb9929ce9670e2e600c951a1bee7f04da Mon Sep 17 00:00:00 2001 From: Kuang-Yu Samuel Chang Date: Wed, 2 Jul 2025 02:30:48 +0200 Subject: [PATCH 7/8] Update equality_constraint_projection_test.py --- .../measurements/equality_constraint_projection_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/openfermion/measurements/equality_constraint_projection_test.py b/src/openfermion/measurements/equality_constraint_projection_test.py index e09856e6e..14b73088c 100644 --- a/src/openfermion/measurements/equality_constraint_projection_test.py +++ b/src/openfermion/measurements/equality_constraint_projection_test.py @@ -13,10 +13,10 @@ import os import unittest import numpy +import openfermion from openfermion.chem import MolecularData from openfermion.config import DATA_DIRECTORY -from openfermion.ops.operators import FermionOperator from openfermion.transforms.opconversions import get_fermion_operator from openfermion.linalg import ( get_sparse_operator, @@ -61,7 +61,7 @@ def test_linearize_term(self): past_terms.add(index) def test_error_with_non_physical_term(self): - non_physical_operator = FermionOperator((0, 1)) + non_physical_operator = openfermion.FermionOperator((0, 1)) with self.assertRaises(ValueError): linearize_term(non_physical_operator, self.n_orbitals) From 164c465c9f9575764ab447673f280128fcc9431a Mon Sep 17 00:00:00 2001 From: Kuang-Yu Samuel Chang Date: Wed, 2 Jul 2025 02:32:15 +0200 Subject: [PATCH 8/8] Update equality_constraint_projection_test.py --- .../measurements/equality_constraint_projection_test.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/openfermion/measurements/equality_constraint_projection_test.py b/src/openfermion/measurements/equality_constraint_projection_test.py index 14b73088c..f1a64fcb5 100644 --- a/src/openfermion/measurements/equality_constraint_projection_test.py +++ b/src/openfermion/measurements/equality_constraint_projection_test.py @@ -13,8 +13,8 @@ import os import unittest import numpy -import openfermion +from openfermion.ops.operators import FermionOperator from openfermion.chem import MolecularData from openfermion.config import DATA_DIRECTORY from openfermion.transforms.opconversions import get_fermion_operator @@ -61,9 +61,9 @@ def test_linearize_term(self): past_terms.add(index) def test_error_with_non_physical_term(self): - non_physical_operator = openfermion.FermionOperator((0, 1)) + non_physical_operator = FermionOperator((0, 1)) with self.assertRaises(ValueError): - linearize_term(non_physical_operator, self.n_orbitals) + linearize_term(non_physical_operator.terms, self.n_orbitals) def test_unlinearize_term_consistency(self): for term, _ in self.fermion_hamiltonian.terms.items():