qgym.envs.scheduling package
Module containing the environment, rewarders, visualizer and other utils for the scheduling problem of OpenQL.
- class qgym.envs.scheduling.BasicRewarder(illegal_action_penalty=-5.0, update_cycle_penalty=-1.0, schedule_gate_bonus=0.0)[source]
Bases:
RewarderBasic rewarder for the
Schedulingenvironment.- __init__(illegal_action_penalty=-5.0, update_cycle_penalty=-1.0, schedule_gate_bonus=0.0)[source]
Initialize the reward range and set the rewards and penalties.
- Parameters:
illegal_action_penalty (
float) – Penalty for performing an illegal action. An action is illegal ifaction[0]is not instate["legal_actions"]. This value should be negative (but is not required) and defaults to -5.update_cycle_penalty (
float) – Penalty given for incrementing a cycle. Since theSchedulingenvironment wants to create the shortest schedules, incrementing the cycle should be penalized. This value should be negative (but is not required) and defaults to -1.schedule_gate_bonus (
float) – Reward gained for successfully scheduling a gate. This value should be positive (but is not required) and defaults to 0.
- compute_reward(*, old_state, action, new_state)[source]
Compute a reward, based on the new state, and the given action. Specifically the ‘legal_actions’ actions array.
- Parameters:
old_state (
SchedulingState) – State of theSchedulingenvironment before the current action.action (
ndarray[Any,dtype[int32]]) – Action that has just been taken.new_state (
SchedulingState) – Updated state of theSchedulingenvironment.
- Return type:
- Returns:
The reward for this action. If the action is illegal, then the reward is illegal_action_penalty. If the action is legal, and increments the cycle, then the reward is update_cycle_penalty. Otherwise, the reward is schedule_gate_bonus.
- class qgym.envs.scheduling.CommutationRulebook(default_rules=True)[source]
Bases:
objectCommutation rulebook used in the
Schedulingenvironment.- __init__(default_rules=True)[source]
Init of the
CommutationRulebook.- Parameters:
default_rules (
bool) – IfTrue, default rules are used. Default rules dictate that gates with disjoint qubits commute and that gates that are exactly the same commute. IfFalse, then no rules will be initialized.
- __repr__()[source]
Create a string representation of the
CommutationRulebook.- Return type:
- commutes(gate1, gate2)[source]
Check if gate1 and gate2 commute according to the rules in the rulebook.
- class qgym.envs.scheduling.EpisodeRewarder(illegal_action_penalty=-5.0, update_cycle_penalty=-1.0)[source]
Bases:
RewarderRewarder for the
Schedulingenvironment, which only gives a reward at the end of the episode or when an illegal action is taken.- __init__(illegal_action_penalty=-5.0, update_cycle_penalty=-1.0)[source]
Initialize the reward range and set the rewards and penalties.
- Parameters:
illegal_action_penalty (
float) – Penalty for performing an illegal action. An action is illegal ifaction[0]is not instate["legal_actions"]. This value should be negative (but is not required) and defaults to -5.update_cycle_penalty (
float) – Penalty given for incrementing a cycle. Since theSchedulingenvironment wants to create the shortest schedules, incrementing the cycle should be penalized. This value should be negative (but is not required) and defaults to -1.
- compute_reward(*, old_state, action, new_state)[source]
Compute a reward, based on the new state, and the given action.
- Parameters:
old_state (
SchedulingState) – State of theSchedulingenvironment before the current action.action (
ndarray[Any,dtype[int32]]) – Action that has just been taken.new_state (
SchedulingState) – Updated state of theSchedulingenvironment.
- Return type:
- Returns:
The reward for this action. If the action is illegal, then the reward is illegal_action_penalty. If the action is legal, but the episode is not yet done, then the reward is 0. Otherwise, the reward is update_cycle_penalty`x`current cycle.
- class qgym.envs.scheduling.MachineProperties(n_qubits)[source]
Bases:
objectMachinePropertiesis a class to conveniently setup machine properties for theSchedulingenvironment.- __init__(n_qubits)[source]
Init of the MachineProperties class.
- Parameters:
n_qubits (
int) – Number of qubits of the machine.
- add_gates(gates)[source]
Add gates to the machine properties that should be supported.
- Parameters:
gates (
Mapping[str,int]) –Mappingof gates that the machine can perform as keys, and the number of machine cycles (time) as values.- Return type:
- Returns:
The
MachinePropertieswith the added gates.
- add_not_in_same_cycle(gates)[source]
Add gates that should not start in the same cycle.
- Parameters:
gates (
Iterable[tuple[str,str]]) –Iterableof tuples of gate names that should not start in the same cycle.- Return type:
- Returns:
The
MachinePropertieswith an updatednot_in_same_cycleproperty. Thenot_in_same_cycleproperty is updated according to the input gates.
- add_same_start(gates)[source]
Add gates that should start in the same cycle, or wait till the previous gate is done.
- Parameters:
gates (
Iterable[str]) –Iterableof gate names that should start in the same cycle.- Return type:
- Returns:
The
MachinePropertieswith the same start gates.
- encode()[source]
Encode the gates in the machine properties to integer values.
- Return type:
- Returns:
The
GateEncoderused to encode the gates. ThisGateEncodercan be used to decode the gates or encode quantum circuits containing the same gate names as in thisMachinePropertiesobject.
- classmethod from_file(filename)[source]
Load MachineProperties from a JSON file. Not implemented.
- Return type:
- classmethod from_mapping(machine_properties)[source]
Initialize the
MachinePropertiesclass from aMappingcontaining valid machines properties.- Parameters:
machine_properties (
Mapping[str,Any]) –Mappingcontaining valid machine properties.- Return type:
- Returns:
Initialized
MachinePropertiesobject with the properties described in the machine_propertiesMapping.
- property gates: dict[str, int] | dict[int, int]
Return a``Dict`` with the gate names the machine can perform as keys, and the number of machine cycles (time) as values.
- class qgym.envs.scheduling.Scheduling(machine_properties, *, max_gates=200, dependency_depth=1, circuit_generator=None, rulebook=None, rewarder=None, render_mode=None)[source]
Bases:
Environment[Dict[str,ndarray[Any,dtype[int32]] |ndarray[Any,dtype[int8]]],ndarray[Any,dtype[int32]]]RL environment for the scheduling problem.
- __init__(machine_properties, *, max_gates=200, dependency_depth=1, circuit_generator=None, rulebook=None, rewarder=None, render_mode=None)[source]
Initialize the action space, observation space, and initial states for the scheduling environment.
- Parameters:
machine_properties (
Mapping[str,Any] |str|MachineProperties) – AMachinePropertiesobject, aMappingcontaining machine properties or a string with a filename for a file containing the machine properties.max_gates (
int) – Maximum number of gates allowed in a circuit. Defaults to 200.dependency_depth (
int) – Number of dependencies given in the observation. Determines the shape of the dependencies observation, which has the shape (dependency_depth, max_gates). Defaults to 1.circuit_generator (
CircuitGenerator|None) – Generator class for generating circuits for training.rulebook (
CommutationRulebook|None) –CommutationRulebookdescribing the commutation rules. IfNone(default) is given, a defaultCommutationRulebookwill be used. (SeeCommutationRulebookfor more info on the default rules.)rewarder (
Rewarder|None) – Rewarder to use for the environment. IfNone(default), then a defaultBasicRewarderis used.render_mode (
str|None) – If"human"open apygamescreen visualizing the step. If"rgb_array", return an RGB array encoding of the rendered frame on each render call.
- get_circuit(mode='human')[source]
Return the quantum circuit of this episode.
- Parameters:
mode (
str) – Choose from be"human"or"encoded". Defaults to"human".- Raises:
ValueError – If an unsupported mode is provided.
- Return type:
- Returns:
Human or encoded quantum circuit.
- reset(*, seed=None, options=None)[source]
Reset the state, action space and load a new (random) initial state.
To be used after an episode is finished.
- Parameters:
seed (
int|None) – Seed for the random number generator, should only be provided (optionally) on the first reset call, i.e., before any learning is done.return_info – Whether to receive debugging info.
options (
Mapping[str,Any] |None) – Mapping with keyword arguments with additional options for the reset. Keywords can be found in the description ofSchedulingState.reset._kwargs – Additional options to configure the reset.
- Return type:
tuple[dict[str,ndarray[Any,dtype[int32]] |ndarray[Any,dtype[int8]]],dict[str,Any]]- Returns:
Initial observation and debugging info.
- class qgym.envs.scheduling.SchedulingState(*, machine_properties, max_gates, dependency_depth, circuit_generator, rulebook)[source]
Bases:
State[Dict[str,ndarray[Any,dtype[int32]] |ndarray[Any,dtype[int8]]],ndarray[Any,dtype[int32]]]The
SchedulingStateclass.- __init__(*, machine_properties, max_gates, dependency_depth, circuit_generator, rulebook)[source]
Init of the
SchedulingStateclass.- Parameters:
machine_properties (
MachineProperties) – AMachinePropertiesobject.max_gates (
int) – Maximum number of gates allowed in a circuit.dependency_depth (
int) – Number of dependencies given in the observation. Determines the shape of the dependencies observation, which has the shape (dependency_depth, max_gates).circuit_generator (
CircuitGenerator) – Generator class for generating circuits for training.rulebook (
CommutationRulebook) –CommutationRulebookdescribing the commutation rules.
- busy
Amount of cycles that a qubit is still busy (zero if available). Used internally for the hardware limitations.
- circuit_info
CircuitInfo`dataclass containing the encoded circuit and attributes used to update the state.
- create_observation_space()[source]
Create the corresponding observation space.
- Return type:
- Returns:
Observation space in the form of a
Dictspace containing:MultiBinaryspace representing the legal actions. If the value at index \(i\) determines if gate number \(i\) can be scheduled or not.MultiDiscretespace representing the integer encoded gate names.MultiDiscretespace representing the interaction of each gate (q1 and q2).MultiDiscretespace representing the first \(n\) gates that must be scheduled before this gate.
- cycle
Current ‘machine’ cycle.
- is_done()[source]
Determine if the state is done or not.
- Return type:
- Returns:
Boolean value stating whether we are in a final state.
- machine_properties
MachinePropertiesclass containing machine properties and limitations.
- reset(*, seed=None, circuit=None, **_kwargs)[source]
Reset the state and load a new (random) initial state.
To be used after an episode is finished.
- Parameters:
seed (
int|None) – Seed for the random number generator, should only be provided (optionally) on the first reset call, i.e., before any learning is done.circuit (
list[Gate] |None) – Optional list of a circuit for the next episode, each entry in the list should be aGate. When a circuit is give, no random circuit will be generated._kwargs (
Any) – Additional options to configure the reset.
- Return type:
- Returns:
Self.
- steps_done: int
Number of steps done since the last reset.
- update_state(action)[source]
Update the state of this environment using the given action.
- utils
SchedulingUtilsdataclass with a random circuit generator, commutation rulebook and a gate encoder.
- qgym.envs.scheduling.machine_properties module
MachinePropertiesMachineProperties.__init__()MachineProperties.__repr__()MachineProperties.__str__()MachineProperties.add_gates()MachineProperties.add_not_in_same_cycle()MachineProperties.add_same_start()MachineProperties.encode()MachineProperties.from_file()MachineProperties.from_mapping()MachineProperties.gatesMachineProperties.n_gatesMachineProperties.n_qubitsMachineProperties.not_in_same_cycleMachineProperties.same_start
- qgym.envs.scheduling.rulebook module
- qgym.envs.scheduling.scheduling module
- qgym.envs.scheduling.scheduling_dataclasses module
- qgym.envs.scheduling.scheduling_rewarders module
- qgym.envs.scheduling.scheduling_state module
SchedulingStateSchedulingState.__init__()SchedulingState.busySchedulingState.circuit_infoSchedulingState.create_observation_space()SchedulingState.cycleSchedulingState.gatesSchedulingState.is_done()SchedulingState.machine_propertiesSchedulingState.obtain_info()SchedulingState.obtain_observation()SchedulingState.reset()SchedulingState.steps_doneSchedulingState.update_state()SchedulingState.utils
- qgym.envs.scheduling.scheduling_visualiser module