Skip to content
Draft
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
30 changes: 21 additions & 9 deletions src/qibo/models/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -761,18 +761,23 @@ def gates_of_type(self, gate: Union[str, type]) -> List[Tuple[int, gates.Gate]]:

return [(i, g) for i, g in enumerate(self.queue) if isinstance(g, gate)]

def _set_parameters_list(self, parameters: ArrayLike, n: int) -> None:
def _set_parameters_list(
self, parameters: ArrayLike, n: int, include_not_trainable: bool = False
) -> None:
"""Helper method for ``set_parameters`` when a list is given.

Also works if ``parameters`` is ``np.ndarray`` or ``tf.Tensor``.
"""
if n == len(self.trainable_gates):
for i, gate in enumerate(self.trainable_gates):
_gates = (
self.parametrized_gates if include_not_trainable else self.trainable_gates
)
if n == len(_gates):
for i, gate in enumerate(_gates):
gate.parameters = parameters[i]
elif n == self.trainable_gates.nparams:
elif n == _gates.nparams:
parameters = list(parameters)
k = 0
for i, gate in enumerate(self.trainable_gates):
for i, gate in enumerate(_gates):
if gate.nparams == 1:
gate.parameters = parameters[i + k]
else:
Expand All @@ -782,11 +787,13 @@ def _set_parameters_list(self, parameters: ArrayLike, n: int) -> None:
raise_error(
ValueError,
f"Given list of parameters has length {n} while "
+ f"the circuit contains {len(self.trainable_gates)} parametrized gates.",
+ f"the circuit contains {len(_gates)} parametrized gates.",
)

def set_parameters(
self, parameters: Union[List[float], Dict[str, float], ArrayLike]
self,
parameters: Union[List[float], Dict[str, float], ArrayLike],
include_not_trainable: bool = False,
) -> None:
"""Updates the parameters of the circuit's parametrized gates.

Expand Down Expand Up @@ -833,10 +840,13 @@ def set_parameters(
params = [0.123, 0.456, 0.789, 0.321]
circuit.set_parameters(params)
"""
_gates = (
self.parametrized_gates if include_not_trainable else self.trainable_gates
)
# reset the final state
self._final_state = None
if isinstance(parameters, dict):
diff = set(parameters.keys()) - self.trainable_gates.set
diff = set(parameters.keys()) - _gates.set
if diff:
raise_error(
KeyError,
Expand All @@ -852,7 +862,9 @@ def set_parameters(
nparams = int(parameters.shape[0])
except AttributeError:
nparams = len(parameters)
self._set_parameters_list(parameters, nparams)
self._set_parameters_list(
parameters, nparams, include_not_trainable=include_not_trainable
)
else:
raise_error(TypeError, f"Invalid type of parameters {type(parameters)}.")

Expand Down
Loading