"""Quantum hardware device interface.Runs MBQC command sequence on quantum hardware."""from__future__importannotationsfromtypingimportTYPE_CHECKING,AnyifTYPE_CHECKING:fromgraphix.patternimportPattern
[docs]classPatternRunner:"""MBQC pattern runner. Executes the measurement pattern. """
[docs]def__init__(self,pattern:Pattern,backend:str="ibmq",**kwargs)->None:"""Instantiate a pattern runner. Parameters ---------- pattern: :class:`graphix.pattern.Pattern` object MBQC pattern to be executed. backend: str execution backend (optional, default is 'ibmq') kwargs: dict keyword args for specified backend. """self.pattern=patternself.backend_name=backendifself.backend_name=="ibmq":try:fromgraphix_ibmq.runnerimportIBMQBackendexceptExceptionase:raiseImportError("Failed to import graphix_ibmq. Please install graphix_ibmq by `pip install graphix-ibmq`.")fromeself.backend=IBMQBackend(pattern)try:instance=kwargs.get("instance","ibm-q/open/main")resource=kwargs.get("resource")save_statevector=kwargs.get("save_statevector",False)optimization_level=kwargs.get("optimizer_level",1)self.backend.get_backend(instance,resource)self.backend.to_qiskit(save_statevector)self.backend.transpile(optimization_level)self.shots=kwargs.get("shots",1024)exceptException:save_statevector=kwargs.get("save_statevector",False)optimization_level=kwargs.get("optimizer_level",1)self.backend.to_qiskit(save_statevector)self.shots=kwargs.get("shots",1024)else:raiseValueError("unknown backend")
defsimulate(self,**kwargs)->Any:"""Perform the simulation. Parameters ---------- kwargs: dict keyword args for specified backend. Returns ------- result: Any the simulation result, in the representation depending on the backend used. """ifself.backend_name=="ibmq":shots=kwargs.get("shots",self.shots)noise_model=kwargs.get("noise_model")format_result=kwargs.get("format_result",True)result=self.backend.simulate(shots=shots,noise_model=noise_model,format_result=format_result)returnresult
[docs]defrun(self,**kwargs)->Any:"""Perform the execution. Parameters ---------- kwargs: dict keyword args for specified backend. Returns ------- result: Any the measurement result, in the representation depending on the backend used. """ifself.backend_name=="ibmq":shots=kwargs.get("shots",self.shots)format_result=kwargs.get("format_result",True)optimization_level=kwargs.get("optimizer_level",1)result=self.backend.run(shots=shots,format_result=format_result,optimization_level=optimization_level)returnresult
[docs]defretrieve_result(self,**kwargs)->Any:"""Retrieve the execution result. Parameters ---------- kwargs: dict keyword args for specified backend. Returns ------- result: Any the measurement result, in the representation depending on the backend used. """ifself.backend_name=="ibmq":job_id=kwargs.get("job_id")result=self.backend.retrieve_result(job_id)returnresult