Skip to content

Commit

Permalink
Trim bug (#305)
Browse files Browse the repository at this point in the history
* minor bug fixes + test
  • Loading branch information
JamesB-1qbit authored May 24, 2023
1 parent e72f2e9 commit c75e261
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
9 changes: 9 additions & 0 deletions tangelo/toolboxes/operators/tests/test_trim_trivial_qubits.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ def test_trim_trivial_qubits(self):
self.assertEqual(ref_circ._gates, trimmed_circuit._gates)
self.assertAlmostEqual(sim.get_expectation_value(trimmed_operator, trimmed_circuit), ref_value, places=5)

def test_trim_trivial_qubits(self):
""" Test if trim trivial qubit function produces correct and compatible circuits and operators """

circ = Circuit(mf_gates + pauli_words_gates, n_qubits=12)
trimmed_operator, trimmed_circuit = trim_trivial_qubits(qb_ham+QubitOperator("Z10"), circ)
self.assertAlmostEqual(np.min(np.linalg.eigvalsh(qubit_operator_sparse(trimmed_operator).todense())), ref_value+1, places=5)
self.assertEqual(ref_circ._gates, trimmed_circuit._gates)
self.assertAlmostEqual(sim.get_expectation_value(trimmed_operator, trimmed_circuit), ref_value+1, places=5)


if __name__ == "__main__":
unittest.main()
8 changes: 6 additions & 2 deletions tangelo/toolboxes/operators/trim_trivial_qubits.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,13 @@ def trim_trivial_circuit(circuit):
# Split circuit and get relevant indices
circs = circuit.split()
e_indices = circuit.get_entangled_indices()
used_qubits = set()
for eq in e_indices:
used_qubits.update(eq)

# Find qubits with no gates applied to them, store qubit index and state |0>
trim_states = {}
for qubit_idx in set(range(circuit.width)) - set(circuit._qubit_indices):
for qubit_idx in set(range(circuit.width)) - used_qubits:
trim_states[qubit_idx] = 0

circuit_new = Circuit()
Expand Down Expand Up @@ -150,7 +153,8 @@ def trim_trivial_circuit(circuit):
circuit_new += circ
else:
circuit_new += circ
return circuit_new, trim_states

return circuit_new, dict(sorted(trim_states.items()))


def trim_trivial_qubits(operator, circuit):
Expand Down

0 comments on commit c75e261

Please sign in to comment.