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

Trim bug #305

Merged
merged 2 commits into from
May 24, 2023
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
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:
ValentinS4t1qbit marked this conversation as resolved.
Show resolved Hide resolved
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