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

Default t_span available for DynamicsBackend.solve #353

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
8 changes: 5 additions & 3 deletions qiskit_dynamics/backend/dynamics_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ def _set_solver(self, solver):
def solve(
self,
solve_input: List[Union[QuantumCircuit, Schedule, ScheduleBlock]],
t_span: ArrayLike,
t_span: Optional[ArrayLike] = None,
y0: Optional[Union[ArrayLike, QuantumState, BaseOperator]] = None,
convert_results: Optional[bool] = True,
validate: Optional[bool] = True,
Expand All @@ -361,7 +361,8 @@ def solve(
``y0`` is not specified, it will be set from ``self.options.initial_state``.

Args:
t_span: Time interval to integrate over.
t_span: Time interval to integrate over. Defaults to ``None``, in which case the
interval is set to ``[[0, input.duration] for input in solve_input]]``.
y0: Initial state.
solve_input: Time evolution of the system in terms of quantum circuits or qiskit
pulse schedules.
Expand All @@ -382,7 +383,8 @@ def solve(
y0 = self.options.initial_state
if isinstance(y0, str) and y0 == "ground_state":
y0 = Statevector(self._dressed_states[:, 0])

if t_span is None:
t_span = [[0, sched.duration * self.dt] for sched in schedules]
solver_results = self.options.solver.solve(
t_span=t_span,
y0=y0,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
upgrade:
- |
``DynamicsBackend.solve()`` method can now work without specifying a ``t_span`` argument.
The default ``t_span`` is set to be ``[0, solve_input.duration]`` for each provided ``solve_input``.
This allows users to solve the dynamics of a quantum circuit without having to specify its
duration in advance.
7 changes: 5 additions & 2 deletions test/dynamics/backend/test_dynamics_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,11 @@ def test_solve(self):
input_variety = [x_sched0, x_circ0]

# solve for all combinations of input types and initial states
for solve_input, (y0, expected_result) in product(input_variety, y0_and_expected_results):
for solve_input, (y0, expected_result), t_span in product(
input_variety, y0_and_expected_results, ([0, n_samples * backend.dt], None)
):
solver_results = backend.solve(
t_span=[0, n_samples * backend.dt],
t_span=t_span,
y0=y0,
solve_input=[solve_input],
)
Expand All @@ -358,6 +360,7 @@ def test_solve(self):
for solver_result in solver_results:
self.assertTrue(solver_result.success)
self.assertAllClose(solver_result.y[-1], expected_result, atol=1e-8, rtol=1e-8)
self.assertEqual(solver_result.t[-1], n_samples * backend.dt)

def test_pi_pulse_initial_state(self):
"""Test simulation of a pi pulse with a different initial state."""
Expand Down
Loading