qgym.envs.scheduling.rulebook module

This module contains the CommutationRulebook class together with basic commutation rules used in the Scheduling environment.

Example

The code block below shows how to set up a CommutationRulebook, with the additional commutation rule that two C-NOT gates with the same control qubit commute.

from qgym.env.scheduling.rulebook import CommutationRulebook

# cnot gates with the same control qubit commute
def cnot_commutation(gate1, gate2):
    if gate1[0] == "cnot" and gate2[0] == "cnot":
        if gate1[1] == gate2[1]:
            return True
    return False

# init the rulebook and add the commutation rule
rulebook = CommutationRulebook()
rulebook.add_rule(rulebook)
class qgym.envs.scheduling.rulebook.CommutationRulebook(default_rules=True)[source]

Bases: object

Commutation rulebook used in the Scheduling environment.

__init__(default_rules=True)[source]

Init of the CommutationRulebook.

Parameters:

default_rules (bool) – If True, default rules are used. Default rules dictate that gates with disjoint qubits commute and that gates that are exactly the same commute. If False, then no rules will be initialized.

__repr__()[source]

Create a string representation of the CommutationRulebook.

Return type:

str

add_rule(rule)[source]

Add a new commutation rule to the rulebook.

Parameters:

rule (Callable[[Gate, Gate], bool]) – Rule to add to the rulebook. A rule is a Callable which takes as input two gates and returns a Boolean value that should be True if two gates commute and False otherwise.

Return type:

None

commutes(gate1, gate2)[source]

Check if gate1 and gate2 commute according to the rules in the rulebook.

Parameters:
  • gate1 (Gate) – Gate to check the commutation.

  • gate2 (Gate) – Gate to check gate1 against.

Return type:

bool

Returns:

Boolean value indicating whether gate1 commutes with gate2.

make_blocking_matrix(circuit)[source]

Make a square array of shape (len(circuit), len(circuit)), with dependencies based on the given commutation rules.

Parameters:

circuit (list[Gate]) – Circuit to check dependencies for.

Return type:

ndarray[Any, dtype[int32]]

Returns:

Dependencies matrix of the circuit based on the rules and scheduling from right to left.

qgym.envs.scheduling.rulebook.disjoint_qubits(gate1, gate2)[source]

Gates that have disjoint qubits commute.

Parameters:
  • gate1 (Gate) – Gate to check disjointness.

  • gate2 (Gate) – Gate to check disjointness against.

Return type:

bool

Returns:

Boolean value stating whether the gates are disjoint.

qgym.envs.scheduling.rulebook.same_gate(gate1, gate2)[source]

Gates that are equal commute.

Parameters:
  • gate1 (Gate) – Gate to check equality.

  • gate2 (Gate) – Gate to check equality against.

Return type:

bool

Returns:

Boolean value stating whether the gates are equal.