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: c = circuit.copy() Transform.RebaseToQiskit().apply(c) physical_c = route(c, arc) physical_c.decompose_SWAP_to_CX() physical_c.redirect_CX_gates(arc) Transform.OptimisePostRouting().apply(physical_c) qc = tk_to_qiskit(physical_c) 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) qc = tk_to_qiskit(c) qobj = assemble(qc) job = self._backend.run(qobj) return np.asarray(job.result().get_statevector(qc, decimals=16))
def get_unitary(self, circuit, fit_to_constraints=True) : """ Obtains the unitary for a given quantum circuit :param circuit: The circuit to inspect :type circuit: Circuit :param fit_to_constraints: Forces the circuit to be decomposed into the correct gate set, defaults to True :type fit_to_constraints: bool, optional :return: The unitary of the circuit. Qubits are ordered with qubit 0 as the least significant qubit """ c = circuit.copy() if fit_to_constraints : Transform.RebaseToQiskit().apply(c) qc = tk_to_qiskit(c) qobj = assemble(qc) job = self._backend.run(qobj) return job.result().get_unitary(qc)
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) qc = tk_to_qiskit(c) 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()}