Skip to content

Commit

Permalink
Separate a code block as a private method
Browse files Browse the repository at this point in the history
  • Loading branch information
itoko committed Oct 7, 2022
1 parent 25919f8 commit 2052eac
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,37 +161,8 @@ def circuits(self) -> List[QuantumCircuit]:
basis_gates = self._get_basis_gates()
self._cliff_utils = CliffordUtils(self.num_qubits, basis_gates=basis_gates) # TODO: cleanup

# Convert interleaved element to transpiled circuit operations and store them for speed
# Convert interleaved element to circuit
if isinstance(self._interleaved_op, QuantumCircuit):
interleaved_circ = self._interleaved_op
elif isinstance(self._interleaved_op, Clifford):
interleaved_circ = self._interleaved_op.to_circuit()
elif isinstance(self._interleaved_op, Gate):
interleaved_circ = QuantumCircuit(self.num_qubits, name=self._interleaved_op.name)
interleaved_circ.append(self._interleaved_op, list(range(self.num_qubits)))
else: # Delay
interleaved_circ = []
if basis_gates and any(i.operation.name not in basis_gates for i in interleaved_circ):
interleaved_circ.name = f"Clifford-{interleaved_circ.name}"
# Transpile circuit with non-basis gates and remove idling qubits
try:
interleaved_circ = transpile(
interleaved_circ, self.backend, **vars(self.transpile_options)
)
except TranspilerError as err:
raise QiskitError("Failed to transpile interleaved_element.") from err
interleaved_circ = _truncate_inactive_qubits(
interleaved_circ, active_qubits=interleaved_circ.qubits[: self.num_qubits]
)
# Convert transpiled circuit to operation
if len(interleaved_circ) == 1:
self._interleaved_op = interleaved_circ.data[0].operation
else:
self._interleaved_op = interleaved_circ
# assert isinstance(self._interleaved_op, (Instruction, QuantumCircuit)
if not isinstance(self._interleaved_op, Instruction):
self._interleaved_op = self._interleaved_op.to_instruction()
# Convert interleaved element to transpiled circuit operation and store it for speed
self.__set_up_interleaved_op(basis_gates)

# Build circuits of reference sequences
reference_sequences = self._sample_sequences()
Expand Down Expand Up @@ -228,3 +199,36 @@ def _to_instruction(
return self._interleaved_op

return super()._to_instruction(elem, basis_gates)

def __set_up_interleaved_op(self, basis_gates: Optional[Tuple[str, ...]]) -> None:
# Convert interleaved element to transpiled circuit operation and store it for speed
# Convert interleaved element to circuit
if isinstance(self._interleaved_op, QuantumCircuit):
interleaved_circ = self._interleaved_op
elif isinstance(self._interleaved_op, Clifford):
interleaved_circ = self._interleaved_op.to_circuit()
elif isinstance(self._interleaved_op, Gate):
interleaved_circ = QuantumCircuit(self.num_qubits, name=self._interleaved_op.name)
interleaved_circ.append(self._interleaved_op, list(range(self.num_qubits)))
else: # Delay
interleaved_circ = []
if basis_gates and any(i.operation.name not in basis_gates for i in interleaved_circ):
interleaved_circ.name = f"Clifford-{interleaved_circ.name}"
# Transpile circuit with non-basis gates and remove idling qubits
try:
interleaved_circ = transpile(
interleaved_circ, self.backend, **vars(self.transpile_options)
)
except TranspilerError as err:
raise QiskitError("Failed to transpile interleaved_element.") from err
interleaved_circ = _truncate_inactive_qubits(
interleaved_circ, active_qubits=interleaved_circ.qubits[: self.num_qubits]
)
# Convert transpiled circuit to operation
if len(interleaved_circ) == 1:
self._interleaved_op = interleaved_circ.data[0].operation
else:
self._interleaved_op = interleaved_circ
# assert isinstance(self._interleaved_op, (Instruction, QuantumCircuit)
if not isinstance(self._interleaved_op, Instruction):
self._interleaved_op = self._interleaved_op.to_instruction()
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ def _sample_sequences(self) -> List[Sequence[SequenceElementType]]:

return sequences

def _get_basis_gates(self) -> Optional[Tuple[str]]:
def _get_basis_gates(self) -> Optional[Tuple[str, ...]]:
"""Get sorted basis gates to use in basis transformation during circuit generation.
Returns:
Expand Down

0 comments on commit 2052eac

Please sign in to comment.