Пример #1
0
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
Пример #2
0
 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])
Пример #3
0
    def _optimise(self): #takes the circuit and optimises it before regurgitating it as a series of ProjectQ commands
        if self._circuit.n_qubits!=0:
            Transform.OptimisePhaseGadgets().apply(self._circuit)
            Transform.RebaseToProjectQ().apply(self._circuit)

        cmd_list = []

        for i in range(self._circuit.n_qubits):
            gate = pqo.Allocate
            cmd = ProjectQCommand(self.main_engine,gate,gate.make_tuple_of_qureg(self._qubit_dictionary[i]))
            cmd_list.append(cmd)

        if self._circuit.n_gates==0:
            return cmd_list
        for command in self._circuit:
            cmd = _get_pq_command_from_tk_command(command,self.main_engine,self._qubit_dictionary)
            cmd_list.append(cmd)
        return cmd_list
Пример #4
0
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
Пример #5
0
 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.RebaseToQuil().apply(c)
     p = tk_to_pyquil(c)
     wf = self._sim.wavefunction(p)
     return wf.amplitudes
Пример #6
0
 def get_state(self,circuit:Circuit, fit_to_constraints=True) -> list:
     c = circuit.copy()
     if fit_to_constraints :
         Transform.RebaseToProjectQ().apply(c)
     fwd = ForwarderEngine(self._backend)
     eng = MainEngine(backend=self._backend,engine_list=[fwd])
     qureg = eng.allocate_qureg(c.n_qubits)
     tk_to_projectq(eng,qureg,c)
     eng.flush()
     state = self._backend.cheat()[1] #`cheat()` returns tuple:(a dictionary of qubit indices, statevector)
     All(Measure) | qureg
     return state #list of complex numbers
Пример #7
0
def projectq_expectation_value(circuit:Circuit,hamiltonian:QubitOperator) -> float :
    ProjectQback = Simulator()
    fwd = ForwarderEngine(ProjectQback)
    eng = MainEngine(backend=ProjectQback,engine_list=[fwd])
    qureg = eng.allocate_qureg(circuit.n_qubits)
    c = circuit.copy()
    Transform.RebaseToProjectQ().apply(c)
    tk_to_projectq(eng,qureg,c)
    eng.flush()
    energy = eng.backend.get_expectation_value(hamiltonian,qureg)
    All(Measure) | qureg
    return energy
Пример #8
0
 def get_operator_expectation_value(self,
                                    state_circuit,
                                    operator,
                                    shots=1000):
     c = state_circuit.copy()
     Transform.RebaseToQuil().apply(c)
     prog = tk_to_pyquil(c)
     pauli_sum = PauliSum([
         _gen_PauliTerm(term, coeff)
         for term, coeff in operator.terms.items()
     ])
     return self._sim.expectation(prog, pauli_sum)
Пример #9
0
    def run(self,
            circuit: Circuit,
            shots: int,
            fit_to_constraints: bool = True) -> np.ndarray:
        """Run a circuit on the Rigetti QVM or a QCS device.

        :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
        :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.
        """
        c = circuit.copy()
        if fit_to_constraints:
            phys_c = route(c, self._architecture)
            phys_c.decompose_SWAP_to_CX()
            Transform.OptimisePostRouting().apply(phys_c)
            Transform.RebaseToQuil().apply(c)
        p = tk_to_pyquil(c)
        p.wrap_in_numshots_loop(shots)
        ex = self._qc.compiler.native_quil_to_executable(p)
        return np.asarray(self._qc.run(ex))
Пример #10
0
    def process_circ(self, circ):
        num_qubits = circ.n_qubits
        if num_qubits == 1 or self.coupling_map == "all-to-all":
            coupling_map = None
        else:
            coupling_map = self.coupling_map
        
        # pre-routing optimise
        Transform.OptimisePhaseGadgets().apply(circ)

        circlay = list(range(num_qubits))

        if coupling_map:
            directed_arc =  Architecture(coupling_map)
            # route_ibm fnction that takes directed Arc, returns dag with cnots etc. 
            circ, circlay = route(circ,directed_arc)
            circ.apply_boundary_map(circlay[0])
        
        # post route optimise
        Transform.OptimisePostRouting().apply(circ)
        circ.remove_blank_wires()

        return circ, circlay
Пример #11
0
    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))
Пример #12
0
 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)
Пример #13
0
    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()}
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())
qc = dag_to_circuit(dag)
print(qc)

energy = 0
for pauli, coeff in qubit_hamiltonian.terms.items():

    shot_exp = backend.get_pauli_expectation_value(circ, pauli, 8000)
    shot_energy = coeff*shot_exp
Пример #15
0
 def get_pauli_expectation_value(self, state_circuit, pauli, shots=1000):
     c = state_circuit.copy()
     Transform.RebaseToQuil().apply(c)
     prog = tk_to_pyquil(c)
     pauli_term = _gen_PauliTerm(pauli)
     return self._sim.expectation(prog, [pauli_term])