Skip to content

Commit

Permalink
Merge pull request #155 from Qyusu/fix-lightling-device
Browse files Browse the repository at this point in the history
fix: device error
  • Loading branch information
kenya-sk authored Jan 12, 2025
2 parents 02dddc5 + 86fb390 commit b9826d6
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 293 deletions.
37 changes: 20 additions & 17 deletions qxmt/devices/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ def __init__(
self.shots = shots
self.random_seed = random_seed
self.logger = logger
self._set_device()

def __call__(self) -> Any:
return self.device
self.real_device = None

def _get_ibmq_real_device(self, backend_name: Optional[str]) -> IBMBackend | BackendV2:
"""Get the IBM Quantum real device.
Expand Down Expand Up @@ -97,7 +94,7 @@ def _set_ibmq_real_device_by_pennylane(self) -> None:
import pennylane as qml

backend = self._get_ibmq_real_device(self.backend_name)
self.device = qml.device(
self.real_device = qml.device(
name=self.device_name,
backend=backend,
wires=backend.num_qubits,
Expand All @@ -108,28 +105,26 @@ def _set_ibmq_real_device_by_pennylane(self) -> None:
f'(backend="{backend.name}", n_qubits={backend.num_qubits}, shots={self.shots})'
)

def _set_simulator_by_pennylane(self) -> None:
def _get_simulator_by_pennylane(self) -> Any:
"""Set PennyLane simulator."""
import pennylane as qml

self.device = qml.device(
return qml.device(
name=self.device_name,
wires=self.n_qubits,
shots=self.shots,
seed=np.random.default_rng(self.random_seed) if self.random_seed is not None else None,
)

def _set_device(self) -> None:
"""Set quantum device.
Raises:
InvalidPlatformError: platform is not implemented.
"""
def get_device(self) -> Any:
if self.platform == "pennylane":
if self.device_name in IBMQ_REAL_DEVICES:
if (self.device_name in IBMQ_REAL_DEVICES) and (self.real_device is None):
self._set_ibmq_real_device_by_pennylane()
return self.real_device
elif (self.device_name in IBMQ_REAL_DEVICES) and (self.real_device is not None):
return self.real_device
else:
self._set_simulator_by_pennylane()
return self._get_simulator_by_pennylane()
else:
raise InvalidPlatformError(f'"{self.platform}" is not implemented.')

Expand Down Expand Up @@ -160,7 +155,11 @@ def get_service(self) -> QiskitRuntimeService:
"""
if self.is_simulator():
raise IBMQSettingError(f'The device ("{self.device_name}") is a simulator.')
return self.device.service

if self.real_device is None:
raise IBMQSettingError(f'The device ("{self.device_name}") is not set.')

return self.real_device.service

def get_backend(self) -> IBMBackend | BackendV2:
"""Get the IBM Quantum real device backend.
Expand All @@ -170,7 +169,11 @@ def get_backend(self) -> IBMBackend | BackendV2:
"""
if self.is_simulator():
raise IBMQSettingError(f'The device ("{self.device_name}") is a simulator.')
return self.device.backend

if self.real_device is None:
raise IBMQSettingError(f'The device ("{self.device_name}") is not set.')

return self.real_device.backend

def get_backend_name(self) -> str:
"""Get the IBM Quantum real device backend name.
Expand Down
3 changes: 2 additions & 1 deletion qxmt/kernels/pennylane/fidelity_kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ def compute(self, x1: np.ndarray, x2: np.ndarray) -> tuple[float, np.ndarray]:
Returns:
tuple[float, np.ndarray]: fidelity kernel value and probability distribution
"""
qnode = qml.QNode(self._circuit, device=self.device(), cache=False) # type: ignore
# qnode = qml.QNode(self._circuit, device=self.device(), cache=False) # type: ignore
qnode = qml.QNode(self._circuit, device=self.device.get_device(), cache=False) # type: ignore
result = qnode(x1, x2)

if self.is_sampling:
Expand Down
2 changes: 1 addition & 1 deletion qxmt/kernels/pennylane/projected_kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def compute(self, x1: np.ndarray, x2: np.ndarray) -> tuple[float, np.ndarray]:
Returns:
tuple[float, np.ndarray]: projected kernel value and probability distribution
"""
qnode = qml.QNode(self._circuit, device=self.device(), cache=False) # type: ignore
qnode = qml.QNode(self._circuit, device=self.device.get_device(), cache=False) # type: ignore
x1_result = qnode(x1)
x2_result = qnode(x2)

Expand Down
9 changes: 6 additions & 3 deletions tests/integration/test_run_experiment_sampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def test_run_experiment_by_sampling_simulator_from_config_file(self, tmp_path: P
id="default.qubit and FidelityKernel",
),
pytest.param(
"lightling.qubit",
"lightning.qubit",
"FidelityKernel",
id="lightling.qubit and FidelityKernel",
),
Expand Down Expand Up @@ -102,9 +102,12 @@ def test_run_experiment_by_sampling_simulator_from_config_instance(
# update config
base_config_path = "tests/integration/configs/state_vector_simulator.yaml"
base_config = ExperimentConfig(path=base_config_path)
config = base_config.model_copy(
update={"device.device_name": device_name, "kernel.implement_name": kernel_name}
updated_device = base_config.device.model_copy(update={"device_name": device_name})
updated_kernel = (
base_config.kernel.model_copy(update={"implement_name": kernel_name}) if base_config.kernel else None
)
config = base_config.model_copy(update={"device": updated_device, "kernel": updated_kernel})

_, _ = experiment.run(config_source=config)

# expected result of each pattern
Expand Down
9 changes: 6 additions & 3 deletions tests/integration/test_run_experiment_state_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def test_run_experiment_by_state_vector_simulator_from_config_file(self, tmp_pat
id="default.qubit and FidelityKernel",
),
pytest.param(
"lightling.qubit",
"lightning.qubit",
"FidelityKernel",
id="lightling.qubit and FidelityKernel",
),
Expand All @@ -100,9 +100,12 @@ def test_run_experiment_by_state_vector_simulator_from_config_instance(
# update config
base_config_path = "tests/integration/configs/state_vector_simulator.yaml"
base_config = ExperimentConfig(path=base_config_path)
config = base_config.model_copy(
update={"device.device_name": device_name, "kernel.implement_name": kernel_name}
updated_device = base_config.device.model_copy(update={"device_name": device_name})
updated_kernel = (
base_config.kernel.model_copy(update={"implement_name": kernel_name}) if base_config.kernel else None
)
config = base_config.model_copy(update={"device": updated_device, "kernel": updated_kernel})

_, _ = experiment.run(config_source=config)

# get result dataframe
Expand Down
Loading

0 comments on commit b9826d6

Please sign in to comment.