#!/usr/bin/env python3 ####################################################################### # convert sw files to an integer signature # hopefully sw files with the same network structure but different operator and ket names have the same signature # Not sure if I can get it to work, but I have an idea to test: # Using: how-many drop exp-max[op] |ket> # and the python: # C.create_universe_inverse() # x.apply_sp_fn(exp_max,context,"op").drop().number_count() # # Author: Garry Morrison # email: garry -at- semantic-db.org # Date: 2016-01-05 # Update: # Copyright: GPLv3 # # Usage: ./sw2sig.py file.sw # # NB: unfinished, and doesn't work! # ####################################################################### import sys import os if len(sys.argv) < 2: print("\nUsage: ./sw2sig.py file.sw\n") sys.exit(1) in_file = sys.argv[1] from the_semantic_db_code import * from the_semantic_db_functions import * from the_semantic_db_processor import * context = context_list("sw file to integer signature") context.load(in_file) context.print_universe(True) # maybe tweak context.print_universe(True) to print_universe(exact). ie, less opaque signature_integer = 1 # find the number of operators in our sw file: ket_list = context.relevant_kets("*") print("ket list:",ket_list) op_list = ket_list.apply_op(context,"supported-ops").ket_sort() print("op list:",op_list) signature_integer *= int(op_list.count_sum()) + 1 #print("\nsignature integer:",signature_integer) #sys.exit(0) # walk the sw file: for x in context.relevant_kets("*"): # find all kets in the sw file print("x:",x) number_of_operators = len(context.recall("supported-ops",x)) print("number of operators:",number_of_operators) signature_integer *= number_of_operators + 1 for op in context.recall("supported-ops",x): # find the supported operators for a given ket op_label = op.label[4:] print("op:",op_label) r = exp_max(x,context,op_label).drop() # find drop exp-max[op] |x> print("r:",r) signature_integer *= len(r) print("\nsignature integer:",signature_integer) sys.exit(0) # walk the sw file: for x in context.relevant_kets("*"): # find all kets in the sw file x_node = x.label.replace('"','\\"') # escape quote characters. for op in context.recall("supported-ops",x): # find the supported operators for a given ket op_label = op.label[4:] arrow_type = "normal" sp = context.recall(op,x) # find the superposition for a given operator applied to the given ket if type(sp) == stored_rule: sp = ket(sp.rule) arrow_type = "box" if type(sp) == memoizing_rule: sp = ket(sp.rule) arrow_type = "tee" for y in sp: y_node = y.label.replace('"','\\"') # escape quote characters. f.write('"%s" -> "%s" [label="%s",arrowhead=%s]\n' % (x_node,y_node,op_label,arrow_type)) # tidy up: f.write("}\n") f.close()