diff --git a/baseline_mps_expectation.py b/baseline_mps_expectation.py index 8985bf7..c1f5fa1 100644 --- a/baseline_mps_expectation.py +++ b/baseline_mps_expectation.py @@ -70,6 +70,8 @@ def main(): parser.add_argument("--exact", action="store_true") parser.add_argument("--exact-max-qubits", type=int, default=24) parser.add_argument("--preprocess", action="store_true") + parser.add_argument("--compile-circuit", action="store_true") + parser.add_argument("--track-memory", action="store_true") args = parser.parse_args() logging.getLogger("qibo.config").setLevel(logging.ERROR) logging.getLogger("qtealeaves").setLevel(logging.ERROR) @@ -105,10 +107,17 @@ def main(): cut_ratio=args.cut_ratio, svd_control=args.svd_control, tensor_module=args.tensor_module, + compile_circuit=args.compile_circuit, + track_memory=args.track_memory, ) start = time.perf_counter() value = float( - backend.expectation(circuit, observable, preprocess=args.preprocess).real + backend.expectation( + circuit, + observable, + preprocess=args.preprocess, + compile_circuit=args.compile_circuit, + ).real ) elapsed = time.perf_counter() - start abs_error = float("nan") if exact is None else abs(value - exact) diff --git a/src/qibotn/backends/qmatchatea.py b/src/qibotn/backends/qmatchatea.py index 4894f8b..6358728 100644 --- a/src/qibotn/backends/qmatchatea.py +++ b/src/qibotn/backends/qmatchatea.py @@ -40,6 +40,9 @@ class QMatchaTeaBackend(QibotnBackend, NumpyBackend): svd_control: str = "A", ini_bond_dimension: int = 1, tensor_module: str = "numpy", + compile_circuit: bool = False, + cache_gate_tensors: bool = True, + track_memory: bool = False, ): """Configure TN simulation given Quantum Matcha Tea interface. @@ -78,6 +81,9 @@ class QMatchaTeaBackend(QibotnBackend, NumpyBackend): ) self.ansatz = ansatz self.tensor_module = tensor_module + self.compile_circuit = compile_circuit + self.cache_gate_tensors = cache_gate_tensors + self.track_memory = track_memory if hasattr(self, "qmatchatea_backend"): self._setup_backend_specifics() @@ -100,6 +106,8 @@ class QMatchaTeaBackend(QibotnBackend, NumpyBackend): ansatz=self.ansatz, tensor_module=self.tensor_module, ) + self.qmatchatea_backend.cache_gate_tensors = self.cache_gate_tensors + self.qmatchatea_backend.track_memory = self.track_memory def execute_circuit( self, @@ -199,7 +207,7 @@ class QMatchaTeaBackend(QibotnBackend, NumpyBackend): statevector=statevector, ) - def expectation(self, circuit, observable, preprocess=True): + def expectation(self, circuit, observable, preprocess=True, compile_circuit=None): """Compute the expectation value of a Qibo-friendly ``observable`` on the Tensor Network constructed from a Qibo ``circuit``. @@ -225,7 +233,11 @@ class QMatchaTeaBackend(QibotnBackend, NumpyBackend): observable = check_observable(observable, circuit.nqubits) # From Qibo to Qiskit - circuit = self._qibocirc_to_qiskitcirc(circuit, preprocess=preprocess) + circuit = self._qibocirc_to_qiskitcirc( + circuit, + preprocess=preprocess, + compile_circuit=compile_circuit, + ) run_qk_params = qmatchatea.preprocessing.qk_transpilation_params(False) operators = qmatchatea.QCOperators() @@ -245,21 +257,28 @@ class QMatchaTeaBackend(QibotnBackend, NumpyBackend): return np.real(results.observables["custom_hamiltonian"]) def _qibocirc_to_qiskitcirc( - self, qibo_circuit, preprocess=True + self, qibo_circuit, preprocess=True, compile_circuit=None ) -> qiskit.QuantumCircuit: """Convert a Qibo Circuit into a Qiskit Circuit.""" # Convert the circuit to QASM 2.0 to qiskit qasm_circuit = qibo_circuit.to_qasm() qiskit_circuit = qiskit.QuantumCircuit.from_qasm_str(qasm_circuit) + if compile_circuit is None: + compile_circuit = self.compile_circuit + if not preprocess: + if compile_circuit: + qiskit_circuit = qmatchatea.tensor_compiler(qiskit_circuit) return qiskit_circuit # Transpile the circuit to adapt it to the linear structure of the MPS, # with the constraint of having only the gates basis_gates qiskit_circuit = qmatchatea.preprocessing.preprocess( qiskit_circuit, - qk_params=qmatchatea.preprocessing.qk_transpilation_params(), + qk_params=qmatchatea.preprocessing.qk_transpilation_params( + tensor_compiler=compile_circuit + ), ) return qiskit_circuit