diff --git a/src/examples/bell_state/host.py b/src/examples/bell_state/host.py index b1e72db16f3d3cdd6cff26443a895e65b2e542d3..c3fd7559db3245d8174c8b019e47b85a68649e3f 100644 --- a/src/examples/bell_state/host.py +++ b/src/examples/bell_state/host.py @@ -6,18 +6,18 @@ from pathlib import Path # qi.set_log_level(logging.DEBUG) qi.set_compiler('mlir') -if qi.connect_backend('pyqcisim_quantumsim') is False: +if qi.connect_backend('symqc_quantumsim') is False: exit(-1) - + qu_file = Path(__file__).parent / "kernel.qu" def routine(circ_name, num_shots=1): - qi.set_num_shots(num_shots) + if not qi.call_quingo(qu_file, circ_name): print("Failed to call {}".format(circ_name)) print("The result of {} is:".format(circ_name)) print(qi.read_result()) -routine("bell_state", 1000) +routine("bell_state") diff --git a/src/quingo/if_backend/backend_hub.py b/src/quingo/if_backend/backend_hub.py index ca9a59c12a57d50f164b482a1b9ebd9b769848c4..10c1ea3cb1aac6c09ddb1c0cb2725f81f6f1913b 100755 --- a/src/quingo/if_backend/backend_hub.py +++ b/src/quingo/if_backend/backend_hub.py @@ -3,8 +3,8 @@ import importlib class Backend_info(): def __init__(self, module_name, module_path, with_timing, is_simulator): - self.module_name = module_name - self.module_path = module_path + self.module_name = module_name #库名字 + self.module_path = module_path #库路径 self.with_timing = with_timing self.is_simulator = is_simulator @@ -23,7 +23,8 @@ class Backend_info(): return module def get_instance(self): - return getattr(self.get_module(), self.module_name)() + #从给定文件中拿出模块对应的名字 + return getattr(self.get_module(), self.module_name)() def singleton(cls): @@ -37,7 +38,7 @@ def singleton(cls): @singleton -class Backend_hub(): +class Backend_hub(): def __init__(self): self.backends = {} @@ -49,9 +50,13 @@ class Backend_hub(): 'PyQCISim_quantumsim', 'pyqcisim_quantumsim', with_timing=False, is_simulator=True) + self.backends['symqc_quantumsim'] = Backend_info( + 'SymQC_quantumsim', 'symqc_quantumsim', with_timing=False, + is_simulator=True) + def support(self, backend_name): return (backend_name in self.backends) def get_instance(self, backend_name): backend_info = self.backends[backend_name] - return backend_info.get_instance() + return backend_info.get_instance() diff --git a/src/quingo/if_backend/non_arch_backend/symqc_quantumsim.py b/src/quingo/if_backend/non_arch_backend/symqc_quantumsim.py new file mode 100644 index 0000000000000000000000000000000000000000..d49f37f1e5a114cd554ff0cd851be8fd9df6ab26 --- /dev/null +++ b/src/quingo/if_backend/non_arch_backend/symqc_quantumsim.py @@ -0,0 +1,64 @@ +from pathlib import Path +from quingo.if_backend.if_backend import If_backend +import quingo.global_config as gc +from quingo.core.utils import * +from symqc import do_sim +# from SymQC import do_sim; +# from symqc.output.symbol_map import symbol_map + + +logger = get_logger((__name__).split('.')[-1]) + + +class SymQC_quantumsim(If_backend): + """A functional QCIS simulation backend using PyQCISim and QuantumSim.""" + + def __init__(self, **kwargs): #关键字参数 + + super().__init__("SymQC_QuantumSim", is_simaultor=True) + self.sim = do_sim.SymQC() + self.pyqcisim_dir = gc.qgrtsys_root_dir / "if_backend" / "symqc" + self.verbose = kwargs.pop('verbose', False) #没有就返回False + self.loglevel = kwargs.pop('loglevel', logging.INFO) #没有就返回logging.INFO + logger.setLevel(self.loglevel) + self.res = None + + def available(self): + return True + + def get_qisa(self): + return "qcis" + + def set_log_level(self, log_level): + self.log_level = log_level + logger.setLevel(self.log_level) + + def set_verbose(self, verbose): + pass + + def upload_program(self, prog_fn, is_binary=False): + assert not is_binary + + if not isinstance(prog_fn, Path): + prog_fn = Path(prog_fn) + + f = prog_fn.open('r').read() #前面的都是读文件的操作,读入f + try: + #此处为compile过程 + self.sim.compiler(f) + return True + except Exception as e: + quingo_err("Error in the QCIS program compiling process of PyQCISim: {}".format(e)) + return False + + def execute(self): + try: + self.res=self.sim.excute() + return True + except Exception as e: + quingo_err("Error in PyQCISim Simulation: {}".format(e)) + return False + + def read_result(self): #这里承接manager.py + """This function tries to read the computation result of the quantum kernel.""" + return self.res