source
CircuitTokenizer
CircuitTokenizer (vocabulary:dict[str,int]|dict[typing.Any,int],
sign_labels:Optional[dict[str,int]]=None)
Helper class that provides a standard way to create an ABC using inheritance.
Test
tensor = torch.tensor([
[1 , 0 ,- 2 ],
[0 , 1 , 2 ],
[0 , 0 ,- 2 ],
], dtype= torch.int32)
params_tensor = torch.tensor([ # ... [max_params, time]
[- 0.9 , 0.9 , 0 ],
[ 0.1 , - 0.7 , 0 ]
])
tokenizer = CircuitTokenizer({"u2" :1 , "ccx" :2 })
instructions = tokenizer.decode(tensor, params_tensor)
instructions.print ()
print (instructions.instruction_names_set)
CircuitInstruction(name='u2', control_nodes=[], target_nodes=[0], params=[0.628318727016449, 6.91150426864624])
CircuitInstruction(name='u2', control_nodes=[], target_nodes=[1], params=[11.9380521774292, 1.8849557638168335])
CircuitInstruction(name='ccx', control_nodes=[0, 2], target_nodes=[1], params=[6.2831854820251465, 6.2831854820251465])
{'u2', 'ccx'}
enc_tensor, enc_params_tensor = tokenizer.encode(instructions)
enc_tensor, enc_params_tensor
(tensor([[ 1, 0, -2],
[ 0, 1, 2],
[ 0, 0, -2]], dtype=torch.int32),
tensor([[-0.9000, 0.9000, 0.0000],
[ 0.1000, -0.7000, 0.0000]]))
assert torch.allclose(tensor, enc_tensor)
assert torch.allclose(params_tensor, enc_params_tensor)
tokenizer = CircuitTokenizer({"u2" :1 , "ccx" :2 })
assert tokenizer.vocabulary == {'u2' : 1 , 'ccx' : 2 }
# test background token checking
tokenizer = CircuitTokenizer({"u2" :0 , "ccx" :1 , "h" :2 , "ry" :3 })
assert tokenizer.vocabulary == {"u2" :1 , "ccx" :2 , "h" :3 , "ry" :4 }
[WARNING]: The value 0 is reserved for background tokens, i.e. qubit time position which are not effected by gates.
[WARNING]: Automatically incrementing all vocabulary values by one ...
print (CircuitTokenizer.get_parametrized_tokens(tokenizer.vocabulary))
assert CircuitTokenizer.get_parametrized_tokens(tokenizer.vocabulary) == [1 , 4 ]
Back to top