Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
Args:
prog1 (strawberryfields.program.Program): quantum program
prog2 (strawberryfields.program.Program): quantum program
compare_params (bool): Set to ``False`` to turn of comparing
program parameters; equivalency will only take into
account the operation order.
atol (float): the absolute tolerance parameter for checking
quantum operation parameter equality
rtol (float): the relative tolerance parameter for checking
quantum operation parameter equality
Returns:
bool: returns ``True`` if two quantum programs are equivalent
"""
DAG1 = list_to_DAG(prog1.circuit)
DAG2 = list_to_DAG(prog2.circuit)
circuit = []
for G in [DAG1, DAG2]:
# relabel the DAG nodes to integers
circuit.append(nx.convert_node_labels_to_integers(G))
# add node attributes to store the operation name and parameters
name_mapping = {i: n.op.__class__.__name__ for i, n in enumerate(G.nodes())}
parameter_mapping = {i: par_evaluate(n.op.p) for i, n in enumerate(G.nodes())}
# CXgate and BSgate are not symmetric wrt to permuting the order of the two
# modes it acts on; i.e., the order of the wires matter
wire_mapping = {}
for i, n in enumerate(G.nodes()):
if n.op.__class__.__name__ == "CXgate":
if np.allclose(n.op.p[0], 0):
Args:
prog1 (strawberryfields.program.Program): quantum program
prog2 (strawberryfields.program.Program): quantum program
compare_params (bool): Set to ``False`` to turn of comparing
program parameters; equivalency will only take into
account the operation order.
atol (float): the absolute tolerance parameter for checking
quantum operation parameter equality
rtol (float): the relative tolerance parameter for checking
quantum operation parameter equality
Returns:
bool: returns ``True`` if two quantum programs are equivalent
"""
DAG1 = list_to_DAG(prog1.circuit)
DAG2 = list_to_DAG(prog2.circuit)
circuit = []
for G in [DAG1, DAG2]:
# relabel the DAG nodes to integers
circuit.append(nx.convert_node_labels_to_integers(G))
# add node attributes to store the operation name and parameters
name_mapping = {i: n.op.__class__.__name__ for i, n in enumerate(G.nodes())}
parameter_mapping = {i: par_evaluate(n.op.p) for i, n in enumerate(G.nodes())}
# CXgate and BSgate are not symmetric wrt to permuting the order of the two
# modes it acts on; i.e., the order of the wires matter
wire_mapping = {}
for i, n in enumerate(G.nodes()):
if n.op.__class__.__name__ == "CXgate":
Args:
prog1 (strawberryfields.program.Program): quantum program
prog2 (strawberryfields.program.Program): quantum program
compare_params (bool): Set to ``False`` to turn of comparing
program parameters; equivalency will only take into
account the operation order.
atol (float): the absolute tolerance parameter for checking
quantum operation parameter equality
rtol (float): the relative tolerance parameter for checking
quantum operation parameter equality
Returns:
bool: returns ``True`` if two quantum programs are equivalent
"""
DAG1 = list_to_DAG(prog1.circuit)
DAG2 = list_to_DAG(prog2.circuit)
circuit = []
for G in [DAG1, DAG2]:
# relabel the DAG nodes to integers
circuit.append(nx.convert_node_labels_to_integers(G))
# add node attributes to store the operation name and parameters
name_mapping = {i: n.op.__class__.__name__ for i, n in enumerate(G.nodes())}
parameter_mapping = {i: par_evaluate(n.op.p) for i, n in enumerate(G.nodes())}
# CXgate and BSgate are not symmetric wrt to permuting the order of the two
# modes it acts on; i.e., the order of the wires matter
wire_mapping = {}
for i, n in enumerate(G.nodes()):
if n.op.__class__.__name__ == "CXgate":
Args:
prog1 (strawberryfields.program.Program): quantum program
prog2 (strawberryfields.program.Program): quantum program
compare_params (bool): Set to ``False`` to turn of comparing
program parameters; equivalency will only take into
account the operation order.
atol (float): the absolute tolerance parameter for checking
quantum operation parameter equality
rtol (float): the relative tolerance parameter for checking
quantum operation parameter equality
Returns:
bool: returns ``True`` if two quantum programs are equivalent
"""
DAG1 = list_to_DAG(prog1.circuit)
DAG2 = list_to_DAG(prog2.circuit)
circuit = []
for G in [DAG1, DAG2]:
# relabel the DAG nodes to integers
circuit.append(nx.convert_node_labels_to_integers(G))
# add node attributes to store the operation name and parameters
name_mapping = {i: n.op.__class__.__name__ for i, n in enumerate(G.nodes())}
parameter_mapping = {i: par_evaluate(n.op.p) for i, n in enumerate(G.nodes())}
# CXgate and BSgate are not symmetric wrt to permuting the order of the two
# modes it acts on; i.e., the order of the wires matter
wire_mapping = {}
for i, n in enumerate(G.nodes()):
if n.op.__class__.__name__ == "CXgate":
if np.allclose(n.op.p[0], 0):
else:
raise ValueError("Could not find target '{}' in the Strawberry Fields circuit database.".format(target))
if db.modes is not None:
# subsystems may be created and destroyed, this is total number that has ever existed
modes_total = len(self.reg_refs)
if modes_total > db.modes:
raise CircuitError(
"This program requires {} modes, but the target '{}' "
"only supports a {}-mode program".format(modes_total, target, db.modes)
)
seq = db.decompose(self.circuit)
if kwargs.get('warn_connected', True):
DAG = pu.list_to_DAG(seq)
temp = nx.algorithms.components.number_weakly_connected_components(DAG)
if temp > 1:
warnings.warn('The circuit consists of {} disconnected components.'.format(temp))
# run optimizations
if kwargs.get('optimize', False):
seq = pu.optimize_circuit(seq)
# does the circuit spec have its own compilation method?
if db.compile is not None:
seq = db.compile(seq, self.register)
# create the compiled Program
compiled = self._linked_copy()
compiled.circuit = seq
compiled.target = target
"""Class-specific circuit compilation method.
If additional compilation logic is required, child classes can redefine this method.
Args:
seq (Sequence[Command]): quantum circuit to modify
registers (Sequence[RegRefs]): quantum registers
Returns:
List[Command]: modified circuit
Raises:
CircuitError: the given circuit cannot be validated to belong to this circuit class
"""
# registers is not used here, but may be used if the method is overwritten pylint: disable=unused-argument
if self.graph is not None:
# check topology
DAG = pu.list_to_DAG(seq)
# relabel the DAG nodes to integers, with attributes
# specifying the operation name. This allows them to be
# compared, rather than using Command objects.
mapping = {i: n.op.__class__.__name__ for i, n in enumerate(DAG.nodes())}
circuit = nx.convert_node_labels_to_integers(DAG)
nx.set_node_attributes(circuit, mapping, name='name')
def node_match(n1, n2):
"""Returns True if both nodes have the same name"""
return n1['name'] == n2['name']
# check if topology matches
if not nx.is_isomorphic(circuit, self.graph, node_match):
# TODO: try and compile the program to match the topology
# TODO: add support for parameter range matching/compilation