Skip to content

Commit 5d4f1b2

Browse files
Add automatic determination of t_span for DynamicsBackend.solve (#353)
Co-authored-by: Daniel Puzzuoli <dan.puzzuoli@gmail.com>
1 parent 1c1bf87 commit 5d4f1b2

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

qiskit_dynamics/backend/dynamics_backend.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ def _set_solver(self, solver):
345345
def solve(
346346
self,
347347
solve_input: List[Union[QuantumCircuit, Schedule, ScheduleBlock]],
348-
t_span: ArrayLike,
348+
t_span: Optional[ArrayLike] = None,
349349
y0: Optional[Union[ArrayLike, QuantumState, BaseOperator]] = None,
350350
convert_results: Optional[bool] = True,
351351
validate: Optional[bool] = True,
@@ -361,7 +361,8 @@ def solve(
361361
``y0`` is not specified, it will be set from ``self.options.initial_state``.
362362
363363
Args:
364-
t_span: Time interval to integrate over.
364+
t_span: Time interval to integrate over. Defaults to ``None``, in which case the
365+
interval is set to ``[[0, input.duration] for input in solve_input]]``.
365366
y0: Initial state.
366367
solve_input: Time evolution of the system in terms of quantum circuits or qiskit
367368
pulse schedules.
@@ -382,7 +383,8 @@ def solve(
382383
y0 = self.options.initial_state
383384
if isinstance(y0, str) and y0 == "ground_state":
384385
y0 = Statevector(self._dressed_states[:, 0])
385-
386+
if t_span is None:
387+
t_span = [[0, sched.duration * self.dt] for sched in schedules]
386388
solver_results = self.options.solver.solve(
387389
t_span=t_span,
388390
y0=y0,
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
upgrade:
3+
- |
4+
``DynamicsBackend.solve()`` method can now work without specifying a ``t_span`` argument.
5+
The default ``t_span`` is set to be ``[0, solve_input.duration]`` for each provided ``solve_input``.
6+
This allows users to solve the dynamics of a quantum circuit without having to specify its
7+
duration in advance.

test/dynamics/backend/test_dynamics_backend.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,9 +347,11 @@ def test_solve(self):
347347
input_variety = [x_sched0, x_circ0]
348348

349349
# solve for all combinations of input types and initial states
350-
for solve_input, (y0, expected_result) in product(input_variety, y0_and_expected_results):
350+
for solve_input, (y0, expected_result), t_span in product(
351+
input_variety, y0_and_expected_results, ([0, n_samples * backend.dt], None)
352+
):
351353
solver_results = backend.solve(
352-
t_span=[0, n_samples * backend.dt],
354+
t_span=t_span,
353355
y0=y0,
354356
solve_input=[solve_input],
355357
)
@@ -358,6 +360,7 @@ def test_solve(self):
358360
for solver_result in solver_results:
359361
self.assertTrue(solver_result.success)
360362
self.assertAllClose(solver_result.y[-1], expected_result, atol=1e-8, rtol=1e-8)
363+
self.assertEqual(solver_result.t[-1], n_samples * backend.dt)
361364

362365
def test_pi_pulse_initial_state(self):
363366
"""Test simulation of a pi pulse with a different initial state."""

0 commit comments

Comments
 (0)