Skip to content

Commit

Permalink
remove unused qubits from layout when split-transpilation is enable i…
Browse files Browse the repository at this point in the history
…n VQE
  • Loading branch information
hhorii committed Jun 11, 2021
1 parent 8948a8f commit bab18b5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
20 changes: 16 additions & 4 deletions qiskit/opflow/converters/circuit_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,18 +313,30 @@ def sample_circuits(
common_circuit = operator[1].to_circuit_op().reduce().to_circuit()
# 1. transpile a common circuit
transpiled_common_circuit = self.quantum_instance.transpile(common_circuit)[0]
layout = transpiled_common_circuit._layout

# 2. transpile diff circuits
diff_circuits = []
for circuit in circuits:
diff_circuit = circuit.copy()
del diff_circuit.data[0 : len(common_circuit)]
diff_circuits.append(diff_circuit)
orig_layout = self.quantum_instance.compile_config["initial_layout"]
self.quantum_instance.compile_config["initial_layout"] = layout

if transpiled_common_circuit._layout is not None:
layout = transpiled_common_circuit._layout.copy()
used_qubits = set([])
for diff_circuit in diff_circuits:
for used_qubit in diff_circuit.qubits:
used_qubits.add(used_qubit)
for q in list(layout.get_virtual_bits().keys()):
if q not in used_qubits:
del layout[q]
orig_layout = self.quantum_instance.compile_config["initial_layout"]
self.quantum_instance.compile_config["initial_layout"] = layout

diff_circuits = self.quantum_instance.transpile(diff_circuits)
self.quantum_instance.compile_config["initial_layout"] = orig_layout

if transpiled_common_circuit._layout is not None:
self.quantum_instance.compile_config["initial_layout"] = orig_layout

# 3. combine
transpiled_circuits = []
Expand Down
4 changes: 2 additions & 2 deletions qiskit/transpiler/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,11 @@ def _set_type_checked_item(self, virtual, physical):

def __delitem__(self, key):
if isinstance(key, int):
del self._p2v[key]
del self._v2p[self._p2v[key]]
del self._p2v[key]
elif isinstance(key, Qubit):
del self._v2p[key]
del self._p2v[self._v2p[key]]
del self._v2p[key]
else:
raise LayoutError(
"The key to remove should be of the form"
Expand Down

0 comments on commit bab18b5

Please sign in to comment.