From 52c37937da02faa6b26e6a786b82b92e07367497 Mon Sep 17 00:00:00 2001 From: Ryosuke Imai Date: Tue, 13 Jul 2021 00:15:23 +0900 Subject: [PATCH 1/3] Fix unexpected behavior of symmetry_conserving_bravyi_kitaev --- .../opconversions/remove_symmetry_qubits.py | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/openfermion/transforms/opconversions/remove_symmetry_qubits.py b/src/openfermion/transforms/opconversions/remove_symmetry_qubits.py index 66f1c00d7..e3c2cd316 100644 --- a/src/openfermion/transforms/opconversions/remove_symmetry_qubits.py +++ b/src/openfermion/transforms/opconversions/remove_symmetry_qubits.py @@ -14,6 +14,8 @@ arXiv:1701.08213 and Phys. Rev. X 6, 031007. """ +import copy + from openfermion.ops.operators import FermionOperator from openfermion.transforms.opconversions import bravyi_kitaev_tree, reorder from openfermion.transforms.repconversions import prune_unused_indices @@ -98,7 +100,7 @@ def symmetry_conserving_bravyi_kitaev(fermion_hamiltonian, active_orbitals, qubit_hamiltonian = edit_hamiltonian_for_spin(qubit_hamiltonian, active_orbitals / 2, parity_middle_orb) - qubit_hamiltonian = prune_unused_indices(qubit_hamiltonian) + qubit_hamiltonian = remove_indices(qubit_hamiltonian, (active_orbitals / 2, active_orbitals)) return qubit_hamiltonian @@ -131,3 +133,23 @@ def edit_hamiltonian_for_spin(qubit_hamiltonian, spin_orbital, orbital_parity): qubit_hamiltonian.compress() return qubit_hamiltonian + + +def remove_indices(symbolic_operator, indices): + """Remove the given indices. + """ + map = {} + def new_index(index): + if index in map: + return map[index] + map[index] = index - len(list(i for i in indices if (i - 1) < index)) + return map[index] + + new_operator = copy.deepcopy(symbolic_operator) + new_operator.terms.clear() + + for term in symbolic_operator.terms: + new_term = [(new_index(op[0]), op[1]) for op in term] + new_operator.terms[tuple(new_term)] = symbolic_operator.terms[term] + + return new_operator From 060abaf74776d9c347bf52263eb8f9c0a75be0b5 Mon Sep 17 00:00:00 2001 From: Ryosuke Imai Date: Tue, 26 Oct 2021 23:34:42 +0900 Subject: [PATCH 2/3] Update docstring --- .../transforms/opconversions/remove_symmetry_qubits.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/openfermion/transforms/opconversions/remove_symmetry_qubits.py b/src/openfermion/transforms/opconversions/remove_symmetry_qubits.py index e3c2cd316..d63f74ec1 100644 --- a/src/openfermion/transforms/opconversions/remove_symmetry_qubits.py +++ b/src/openfermion/transforms/opconversions/remove_symmetry_qubits.py @@ -136,7 +136,14 @@ def edit_hamiltonian_for_spin(qubit_hamiltonian, spin_orbital, orbital_parity): def remove_indices(symbolic_operator, indices): - """Remove the given indices. + """Returns the symbolic operator from which the operator with the specified index was removed. + + Args: + symbolic_operator: An instance of the SymbolicOperator class. + indices: A sequence of Int type object. The indices to be removed. + + Returns: + The symbolic operator. The removed indices will be filled by shifting. """ map = {} def new_index(index): From f792a0d2c854177a582040ac5296caf69bd730f7 Mon Sep 17 00:00:00 2001 From: Ryosuke Imai Date: Tue, 2 Nov 2021 14:28:32 +0900 Subject: [PATCH 3/3] fix lint errors --- .../opconversions/remove_symmetry_qubits.py | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/openfermion/transforms/opconversions/remove_symmetry_qubits.py b/src/openfermion/transforms/opconversions/remove_symmetry_qubits.py index d63f74ec1..09ae27a6f 100644 --- a/src/openfermion/transforms/opconversions/remove_symmetry_qubits.py +++ b/src/openfermion/transforms/opconversions/remove_symmetry_qubits.py @@ -18,7 +18,6 @@ from openfermion.ops.operators import FermionOperator from openfermion.transforms.opconversions import bravyi_kitaev_tree, reorder -from openfermion.transforms.repconversions import prune_unused_indices from openfermion.utils.indexing import up_then_down @@ -63,12 +62,12 @@ def symmetry_conserving_bravyi_kitaev(fermion_hamiltonian, active_orbitals, """ # Catch errors if inputs are of wrong type. if type(fermion_hamiltonian) is not FermionOperator: - raise ValueError('Supplied operator should be an instance ' - 'of FermionOperator class') + raise ValueError( + "Supplied operator should be an instance of FermionOperator class") if type(active_orbitals) is not int: - raise ValueError('Number of active orbitals should be an integer.') + raise ValueError("Number of active orbitals should be an integer.") if type(active_fermions) is not int: - raise ValueError('Number of active fermions should be an integer.') + raise ValueError("Number of active fermions should be an integer.") # Arrange spins up then down, then BK map to qubit Hamiltonian. fermion_hamiltonian_reorder = reorder(fermion_hamiltonian, @@ -100,7 +99,8 @@ def symmetry_conserving_bravyi_kitaev(fermion_hamiltonian, active_orbitals, qubit_hamiltonian = edit_hamiltonian_for_spin(qubit_hamiltonian, active_orbitals / 2, parity_middle_orb) - qubit_hamiltonian = remove_indices(qubit_hamiltonian, (active_orbitals / 2, active_orbitals)) + qubit_hamiltonian = remove_indices(qubit_hamiltonian, + (active_orbitals / 2, active_orbitals)) return qubit_hamiltonian @@ -112,9 +112,9 @@ def edit_hamiltonian_for_spin(qubit_hamiltonian, spin_orbital, orbital_parity): for term, coefficient in qubit_hamiltonian.terms.items(): # If Z acts on the specified orbital, precompute its effect and # remove it from the Hamiltonian. - if (spin_orbital - 1, 'Z') in term: + if (spin_orbital - 1, "Z") in term: new_coefficient = coefficient * orbital_parity - new_term = tuple(i for i in term if i != (spin_orbital - 1, 'Z')) + new_term = tuple(i for i in term if i != (spin_orbital - 1, "Z")) # Make sure to combine terms comprised of the same operators. if new_qubit_dict.get(new_term) is None: new_qubit_dict[new_term] = new_coefficient @@ -136,7 +136,8 @@ def edit_hamiltonian_for_spin(qubit_hamiltonian, spin_orbital, orbital_parity): def remove_indices(symbolic_operator, indices): - """Returns the symbolic operator from which the operator with the specified index was removed. + """Returns the symbolic operator from which the operator with the specified + index was removed. Args: symbolic_operator: An instance of the SymbolicOperator class. @@ -146,10 +147,11 @@ def remove_indices(symbolic_operator, indices): The symbolic operator. The removed indices will be filled by shifting. """ map = {} + def new_index(index): if index in map: return map[index] - map[index] = index - len(list(i for i in indices if (i - 1) < index)) + map[index] = index - len(list(i for i in indices if (i - 1) < index)) return map[index] new_operator = copy.deepcopy(symbolic_operator)