# This code is part of vulqano.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.
"""
Define and generate abstract transformation rules linking equivalent
subcircuits that involve discrete gates.
A rule is described by a couple subcircuits. Each subciruits is
described by nested lists of strings, corresponding to n-dimensional arrays
of strings. The first index labels the time-step, the other indices label the
position of the qubit in the lattice. Each string denote the name of the
corresponding gate (see vulqano.gates.discretegates). The string "any"
indicates that the corresponding gate is an arbitrary gate, while the string
"any_rot" indicates that the corresponding gate is an arbitrary R_z rotation.
"""
import copy
import numpy as np
from vulqano.states import AbstractCircuitState
from vulqano.utils import (
check_circuit_equivalence,
)
from vulqano.gates.discretegates import (
GATES_DICTIONARY,
)
from vulqano.rules.standarddiscreterules import STD_DISCRETE_RULES_GENERATORS
from vulqano.rules.sym4rules import SYM_4_RULES_GENERATORS
__all__ = [
"DiscreteTransformationRule",
"generate_discrete_abstract_rules",
]
def rotate_state(state_vector):
"""
Rotate a state: [A, B, C] -> [[A, B, C]] and replaces eventual two-qubits
gates with their rotated form.
**Arguments**
state_vector : np.array
State to be rotated (GATES description)
**Returns**
state_vector : np.array
Rotated state
"""
state_vector = np.transpose(state_vector, (0, 2, 1))
for idx, gate in np.ndenumerate(state_vector):
if GATES_DICTIONARY[gate]["Connectivity"] == [1]:
state_vector[idx] += "_r"
return state_vector
[docs]
def generate_discrete_abstract_rules(
gates, dim, rules_classes="all", verify=False, generators="std"
):
"""
Returns a list of discrete transformation rules.
**Arguments**
gates : set
The set of gates involved in the transformation rules.
dim : int
Number of dimensions of the qubits lattice.
rules_classes : str or list of ints, optional
A list of the ints identifying the rule classes that we want to generate.
Default is "all" and generates all the rule classes.
verify : bool, optional
If true, each rule is tested. Default is False.
generators : "std" or list of generators, optional
The list of generators producing the rules to be used. Default is "std",
in this case a standard list of rules is used.
**Returns**
rules : list of DiscreteTransformationRule(s)
The generated list of transition rules.
"""
if dim > 3:
raise NotImplementedError("Maximum implemented lattice dimension is 2.")
gates_extended = copy.deepcopy(gates)
gates_extended.add("idle")
gates_extended.add("busy")
gates_extended.add("any")
gates_extended.add("any_rot")
if generators == "std":
generators = STD_DISCRETE_RULES_GENERATORS
rules = []
if rules_classes == "all":
rules_classes = list(range(len(generators)))
if set(rules_classes).issubset(set(range(len(generators)))) is not True:
raise ValueError(
"rules_classes must be a list of int from 0 to "
+ str(len(generators) - 1)
+ ' or "all".'
)
for class_index in rules_classes:
for rule in generators[class_index]():
state_a = np.array(rule[0], dtype=object)
state_b = np.array(rule[1], dtype=object)
if len(state_a.shape) == dim:
abstract_rule = DiscreteTransformationRule(
np.copy(state_a),
np.copy(state_b),
class_index,
verify,
)
if abstract_rule.involved_gates.issubset(gates_extended):
rules.append(abstract_rule)
if len(state_a.shape) == 2 and dim == 3:
# [A, B, C] -> [[A, B, C]]
state_a = np.expand_dims(state_a, 1)
state_b = np.expand_dims(state_b, 1)
abstract_rule = DiscreteTransformationRule(
np.copy(state_a),
np.copy(state_b),
class_index,
verify,
)
if abstract_rule.involved_gates.issubset(gates_extended):
rules.append(abstract_rule)
if state_a.shape[-1] > 1:
# [[A, B, C]] -> [[A], [B], [C]]
state_a = rotate_state(state_a)
state_b = rotate_state(state_b)
abstract_rule = DiscreteTransformationRule(
np.copy(state_a),
np.copy(state_b),
class_index,
verify,
)
if abstract_rule.involved_gates.issubset(gates_extended):
rules.append(abstract_rule)
return rules
def unit_test_2d():
"""
Unit test to check if all the rules are valid.
**Returns**
: bool
True if all the rules are valid.
"""
generate_discrete_abstract_rules(
{
"T",
"H",
"CZ",
"SWAP",
"S",
"Z",
"Tdg",
"Sdg",
"RZ5",
"RZ5dg",
"RZ6",
"RZ6dg",
},
2,
verify=True,
)
return True
def unit_test_3d():
"""
Unit test to check if all the rules are valid.
**Returns**
: bool
True if all the rules are valid.
"""
generate_discrete_abstract_rules(
{
"T",
"H",
"CZ",
"CZ_r",
"SWAP",
"SWAP_r",
"S",
"Z",
"Tdg",
"Sdg",
"RZ5",
"RZ5dg",
"RZ6",
"RZ6dg",
},
3,
verify=True,
)
return True
def unit_test_sym4():
"""
Unit test to check if all the rules are valid.
**Returns**
: bool
True if all the rules are valid.
"""
generate_discrete_abstract_rules(
{
"H",
"CZ",
"SWAP",
},
2,
verify=True,
generators=SYM_4_RULES_GENERATORS,
)
return True
if __name__ == "__main__":
print(unit_test_2d())
print(unit_test_3d())