def sample(self, circuit, ref_array, n_samples=1024): if not isinstance(circuit, model.GateList): ops = circuit circuit = model.GateList() circuit.set(ops) obs = np.empty([n_samples], dtype=np.int64) self.reset() preprocessed = self.preprocessor.preprocess(circuit) for loop in range(n_samples): self.reset() self._value_store.sync_refs(self.preprocessor.get_refset()) self.op_iter = GateListIterator(preprocessed.ops) while True: op = self.op_iter.next() if op is None: break if isinstance(op, model.IfClause): if self._evaluate_if(op): self.op_iter.prepend(op.clause) else: self.executor.enqueue(op) self.executor.flush() packed_value = self._value_store.get_packed_value(ref_array) obs[loop] = packed_value for qstates in self.qubits.qstates_list: qstates.processor.synchronize() mask = self._value_store.get_mask(ref_array) return ObservationList(ref_array, obs, mask)
def run(self, circuit): if not isinstance(circuit, model.GateList): ops = circuit circuit = model.GateList() circuit.set(ops) preprocessed = self.preprocessor.preprocess(circuit) # model.dump(preprocessed) self._value_store.sync_refs(self.preprocessor.get_refset()) self.op_iter = GateListIterator(preprocessed.ops) while True: op = self.op_iter.next() if op is None: break if isinstance(op, model.IfClause): if self._evaluate_if(op): self.op_iter.prepend(op.clause) else: self.executor.enqueue(op) self.executor.flush() self._qubits.update_external_layout() for qstates in self.qubits.qstates_list: qstates.processor.synchronize()
def prepend(self, op): # If op is not a clause, envelop them with a new clause. if not isinstance(op, model.GateList): clause = model.GateList() clause.set(op) # FIXME: no copy op = clause # create a new frame for clause self.op_iter = iter([FrameBegin()] + op.ops) self.op_iter_stack.append(self.op_iter)
def run(self, circuit): if not isinstance(circuit, model.GateList): ops = circuit circuit = model.GateList() circuit.set(ops) expanded = expand_clauses(circuit) self.preprocessor.preprocess(expanded) self.prepare() self.op_iter = OperatorIterator(expanded.ops) while self.run_step(): pass
def if_(refs, cond, clause): refs = _expand_args(refs) gatelist = model.GateList() gatelist.set(clause) if_clause = model.IfClause(refs, cond, gatelist) return if_clause
def new_gatelist(): return model.GateList()