qgym.utils.gate_encoder module
This module contains the GateEncoder
class which can be used for encoding gates
to integers and back.
- Usage:
>>> from qgym.utils import GateEncoder >>> encoder = GateEncoder().learn_gates(["x", "y", "z", "cnot", "measure"]) >>> encoded_list = encoder.encode_gates(["x", "x", "measure", "z"]) >>> print(encoded_list) [1, 1, 5, 3] >>> encoder.decode_gates(encoded_list) ['x', 'x', 'measure', 'z']
- class qgym.utils.gate_encoder.GateEncoder[source]
Bases:
object
Learns a set of gates and creates a mapping to integers and back.
- decode_gates(encoded_gates)[source]
Decode integer encoded gate names to the original gate names based on the gates seen in
learn_gates
.- Parameters:
- Raises:
TypeError – When an unsupported type is given
- Return type:
- Returns:
Decoded version of encoded_gates. The output structure should resemble the input structure. So a
Mapping
will return aDict
, while a singleint
will return astr
.
- encode_gates(gates)[source]
Encode the gate names (of type
str
) in gates to integers, based on the gates seen inlearn_gates
.- Parameters:
gates (
str
|Mapping
[str
,Any
] |Sequence
[Gate
] |set
[str
] |list
[str
] |tuple
[str
,...
]) – Gates to encode. The input type determines the return type.- Raises:
TypeError – When an unsupported type is given.
- Return type:
- Returns: Integer encoded version of gates. The output structure should resemble
the input structure. So a
Mapping
will return aDict
, while a singlestr
will return anint
.
- learn_gates(gates)[source]
Learns the gates names from an
Iterable
and creates a mapping from unique gate names to integers and back.