def get_counts(self, circuit, shots, fit_to_constraints=True): """ Run the circuit on the backend and accumulate the results into a summary of counts :param circuit: The circuit to run :param shots: Number of shots (repeats) to run :param fit_to_constraints: Compile the circuit to meet the constraints of the backend, defaults to True :param seed: Random seed to for simulator :return: Dictionary mapping bitvectors of results to number of times that result was observed (zero counts are omitted) """ if fit_to_constraints: qc = _routed_ibmq_circuit(circuit, self.architecture) else: dag = tk_to_dagcircuit(circuit) qc = dag_to_circuit(dag) valid, measures = _qiskit_circ_valid(qc, self.coupling) if not valid: raise RuntimeWarning( "QuantumCircuit does not pass validity test, will likely fail on remote backend." ) if not measures: raise RuntimeWarning("Measure gates are required for output.") qobj = assemble(qc, shots=shots) job = self._backend.run(qobj) counts = job.result().get_counts(qc) return {tuple(_convert_bin_str(b)): c for b, c in counts.items()}
def run(self, circuit: Circuit, shots: int, fit_to_constraints: bool = True) -> np.ndarray: if fit_to_constraints: qc = _routed_ibmq_circuit(circuit, self.architecture) else: dag = tk_to_dagcircuit(circuit) qc = dag_to_circuit(dag) valid, measures = _qiskit_circ_valid(qc, self.coupling) if not valid: raise RuntimeWarning( "QuantumCircuit does not pass validity test, will likely fail on remote backend." ) if not measures: raise RuntimeWarning("Measure gates are required for output.") qobj = assemble(qc, shots=shots, memory=self.config.memory) job = self._backend.run(qobj) if self._monitor: job_monitor(job) shot_list = [] if self.config.memory: shot_list = job.result().get_memory(qc) else: for string, count in job.result().get_counts().items(): shot_list += [string] * count return np.asarray([_convert_bin_str(shot) for shot in shot_list])
def run(self, circuit: Circuit, shots: int, fit_to_constraints=True, seed: int = None) -> np.ndarray: """Run a circuit on Qiskit Aer Qasm simulator. :param circuit: The circuit to run :type circuit: Circuit :param shots: Number of shots (repeats) to run :type shots: int :param fit_to_constraints: Compile the circuit to meet the constraints of the backend, defaults to True :type fit_to_constraints: bool, optional :param seed: random seed to for simulator :type seed: int :return: Table of shot results, each row is a shot, columns are ordered by qubit ordering. Values are 0 or 1, corresponding to qubit basis states. :rtype: numpy.ndarray """ c = circuit.copy() if fit_to_constraints: Transform.RebaseToQiskit().apply(c) dag = tk_to_dagcircuit(c) qc = dag_to_circuit(dag) qobj = assemble(qc, shots=shots, seed_simulator=seed, memory=True) job = self._backend.run(qobj, noise_model=self.noise_model) shot_list = job.result().get_memory(qc) return np.asarray([_convert_bin_str(shot) for shot in shot_list])
def routed_ibmq_circuit(circuit: Circuit, arc: Architecture) -> QuantumCircuit: physical_c = route(circuit, arc) physical_c.decompose_SWAP_to_CX() physical_c.redirect_CX_gates(arc) Transform.OptimisePostRouting().apply(physical_c) dag = tk_to_dagcircuit(physical_c) qc = dag_to_circuit(dag) return qc
def get_state(self, circuit, fit_to_constraints=True) : """ Calculate the statevector for a circuit. :param circuit: circuit to calculate :return: complex numpy array of statevector """ c = circuit.copy() if fit_to_constraints : Transform.RebaseToQiskit().apply(c) dag = tk_to_dagcircuit(c) qc = dag_to_circuit(dag) qobj = assemble(qc) job = self._backend.run(qobj) return np.asarray(job.result().get_statevector(qc, decimals=16))
def run(self, dag:DAGCircuit) -> DAGCircuit: """ Run one pass of optimisation on the circuit and route for the given backend. :param dag: The circuit to optimise and route :return: The modified circuit """ circ = dagcircuit_to_tk(dag, _DROP_CONDS=self.DROP_CONDS,_BOX_UNKNOWN=self.BOX_UNKNOWN) circ, circlay = self.process_circ(circ) newdag = tk_to_dagcircuit(circ) newdag.name = dag.name finlay = dict() for i, qi in enumerate(circlay): finlay[('q', i)] = ('q', qi) newdag.final_layout = finlay return newdag
def get_counts(self, circuit, shots, fit_to_constraints=True, seed=None) : """ Run the circuit on the backend and accumulate the results into a summary of counts :param circuit: The circuit to run :param shots: Number of shots (repeats) to run :param fit_to_constraints: Compile the circuit to meet the constraints of the backend, defaults to True :param seed: Random seed to for simulator :return: Dictionary mapping bitvectors of results to number of times that result was observed (zero counts are omitted) """ c = circuit.copy() if fit_to_constraints : Transform.RebaseToQiskit().apply(c) dag = tk_to_dagcircuit(c) qc = dag_to_circuit(dag) qobj = assemble(qc, shots=shots, seed_simulator=seed) job = self._backend.run(qobj, noise_model=self.noise_model) counts = job.result().get_counts(qc) return {tuple(_convert_bin_str(b)) : c for b, c in counts.items()}
n_qubits = int(math.log(dim, 2)) converter.n_qubits = n_qubits n_cnots = (n_layers // 2) * (n_qubits // 2) + ((n_qubits - 1) // 2) * ( (n_layers + 1) // 2) r = RewriteTket(Circuit(n_qubits), [], []) r.set_target_unitary(unitary) skips = [] circuit, distance = approximate_scipy(unitary, n_layers, r, []) for k in range(10): new_skip = random.choice([i for i in range(n_cnots) if i not in skips]) print("maybe I will skip", new_skip) new_circ, new_d = approximate_scipy(unitary, n_layers, r, skips + [new_skip]) if new_d <= distance: # or random.random() < (1 - new_d + distance) / (5 * k + 1): print("skipping", new_skip) circuit, distance, skips = new_circ, new_d, skips + [new_skip] else: print("not skipping", new_skip) print(circuit.n_gates, circuit.n_gates_of_type(OpType.CX)) if distance < 0.01: print("returning early", distance) return circuit, r.fidelity(circuit) print(distance) return circuit, r.fidelity(circuit) circuit, fid = approximate_scipy(unitary, n_layers, r) print("final fidelity", fid) print(circuit.n_gates, circuit.n_gates_of_type(OpType.CX)) print(dag_to_circuit(tk_to_dagcircuit(circuit)))
alpha_electrons = 1 beta_electrons = 1 #qubit_generator = uccd_general_evolution(packed_amplitudes, alpha_electrons, beta_electrons, n_qubits) circ = Circuit(n_qubits) hf_wavefunction(circ, n_electrons, multiplicity) print (qubit_generator) for pauli, coeff in qubit_generator.terms.items(): pauli_evolution(pauli, coeff, circ) print("Pre-Opt Qasm Circuit") dag = tk_to_dagcircuit(circ) print(dag.qasm(qeflag=True)) print(circ.depth()) qc = dag_to_circuit(dag) print(qc) #optimise_pre_routing(circ) Transform.get_transform_instance("PauliGadget_Opt").apply(circ) optimise_post_routing(circ) print("Post-Opt Qasm Circuit") dag = tk_to_dagcircuit(circ) print(dag.qasm(qeflag=True)) print(circ.depth())