qiskit-community / qiskit-dynamics

Tools for building and solving models of quantum systems in Qiskit
https://qiskit-community.github.io/qiskit-dynamics/
Apache License 2.0
105 stars 61 forks source link

`DynamicsBackend` sets `Target.num_qubits` which is not settable in Qiskit 1.3 #366

Open wshanks opened 6 days ago

wshanks commented 6 days ago

Informations

What is the current behavior?

AttributeError when trying to create a DynamicsBackend:


File ~/envs/qiskit/lib/python3.12/site-packages/qiskit_dynamics/backend/dynamics_backend.py:219, in DynamicsBackend.__init__(self, solver, target, **options)
    216     target.add_instruction(Measure(), measure_properties)
    218 target.dt = solver._dt
--> 219 target.num_qubits = len(self.options.subsystem_dims)
    221 self._target = target

AttributeError: attribute 'num_qubits' of 'qiskit._accelerate.target.BaseTarget' objects is not writable

Steps to reproduce the problem

Run the following code with Qiskit 1.3:

from qiskit.quantum_info import Operator
from qiskit_dynamics import DynamicsBackend, Solver
import numpy as np

nu_z = 10.
nu_x = 1.
nu_d = 9.98 # Almost on resonance with the Hamiltonian's energy levels difference, nu_z

X = Operator.from_label('X')
Y = Operator.from_label('Y')
Z = Operator.from_label('Z')
s_p = 0.5 * (X + 1j * Y)

solver = Solver(
    static_hamiltonian=.5 * 2 * np.pi * nu_z * Z,
    hamiltonian_operators=[2 * np.pi * nu_x * X],
    hamiltonian_channels=["d0"],
    channel_carrier_freqs={"d0": nu_x},
    dt=0.01,
)

dynamics_backend = DynamicsBackend(solver=solver, subsystem_dims=[2])

What is the expected behavior?

Successful DynamicsBackend creation

Suggested solutions

num_qubits can be passed to Target instead. I am not sure about the case when a target is passed in to DynamicsBackend. ~I think it should just leave the target alone and base the number of qubits on the subsystem dimensions. Perhaps it could make a new target and only copy the instructions related to the qubits in the subsystem.~ Thinking about it more, I think it would be most consistent to build a new qubit with just the instructions for the qubits in the subsystem to be simulated. Otherwise, number of qubits will be inconsistent in different parts of the code.