Skip to content

Commit

Permalink
transpile only ansatz with default
Browse files Browse the repository at this point in the history
  • Loading branch information
hhorii committed May 24, 2021
1 parent c795068 commit f4aec15
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
12 changes: 10 additions & 2 deletions qiskit/algorithms/minimum_eigen_solvers/vqe.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ def __init__(
max_evals_grouped: int = 1,
callback: Optional[Callable[[int, np.ndarray, float, float], None]] = None,
quantum_instance: Optional[Union[QuantumInstance, BaseBackend, Backend]] = None,
split_transpile: bool = False,
) -> None:
"""
Expand Down Expand Up @@ -139,6 +140,8 @@ def __init__(
These are: the evaluation count, the optimizer parameters for the
ansatz, the evaluated mean and the evaluated standard deviation.`
quantum_instance: Quantum Instance or Backend
split_transpile: ``True`` allows transpiling circuits for ansatz and expectation
separately.
"""
validate_min("max_evals_grouped", max_evals_grouped, 1)
if ansatz is None:
Expand All @@ -156,6 +159,7 @@ def __init__(
self._expectation = expectation
self._user_valid_expectation = self._expectation is not None
self._include_custom = include_custom
self._split_transpile = split_transpile
self._expect_op = None

super().__init__(
Expand Down Expand Up @@ -197,7 +201,8 @@ def quantum_instance(
super(VQE, self.__class__).quantum_instance.__set__(self, quantum_instance)

self._circuit_sampler = CircuitSampler(
self._quantum_instance, param_qobj=is_aer_provider(self._quantum_instance.backend)
self._quantum_instance, param_qobj=is_aer_provider(self._quantum_instance.backend),
skip_transpile=self._split_transpile
)

@property
Expand Down Expand Up @@ -317,7 +322,10 @@ def construct_expectation(
)

observable_meas = self.expectation.convert(StateFn(operator, is_measurement=True))
ansatz_circuit_op = CircuitStateFn(wave_function)
if self._split_transpile:
ansatz_circuit_op = CircuitStateFn(self.quantum_instance.transpile(wave_function)[0])
else:
ansatz_circuit_op = CircuitStateFn(wave_function)
return observable_meas.compose(ansatz_circuit_op).reduce()

def construct_circuit(
Expand Down
7 changes: 6 additions & 1 deletion qiskit/opflow/converters/circuit_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def __init__(
param_qobj: bool = False,
attach_results: bool = False,
caching: str = "last",
skip_transpile: bool = False,
) -> None:
"""
Args:
Expand Down Expand Up @@ -97,6 +98,7 @@ def __init__(
self._transpiled_circ_cache: Optional[List[Any]] = None
self._transpiled_circ_templates: Optional[List[Any]] = None
self._transpile_before_bind = True
self._skip_transpile = skip_transpile

def _check_quantum_instance_and_modes_consistent(self) -> None:
"""Checks whether the statevector and param_qobj settings are compatible with the
Expand Down Expand Up @@ -294,7 +296,10 @@ def sample_circuits(
circuits = [op_c.to_circuit(meas=True) for op_c in circuit_sfns]

try:
self._transpiled_circ_cache = self.quantum_instance.transpile(circuits)
if self._skip_transpile:
self._transpiled_circ_cache = circuits
else:
self._transpiled_circ_cache = self.quantum_instance.transpile(circuits)
except QiskitError:
logger.debug(
r"CircuitSampler failed to transpile circuits with unbound "
Expand Down

0 comments on commit f4aec15

Please sign in to comment.