예제 #1
0
    def __init__(self, api: QuantumInspireAPI, provider: Any,
                 configuration: Optional[QasmBackendConfiguration] = None) -> None:
        """ Python implementation of a quantum simulator using Quantum Inspire API.

        Args:
            api: The interface instance to the Quantum Inspire API.
            provider: Provider for this backend.
            configuration: The configuration of the quantum inspire backend. The
                configuration must implement the fields given by the QiSimulatorPy.DEFAULT_CONFIGURATION. All
                configuration fields are listed in the table below. The table rows with an asterisk specify fields which
                can have a custom value and are allowed to be changed according to the description column.

                | key                     | description
                |-------------------------|----------------------------------------------------------------------------
                | backend_name (str)*     | The name of the quantum inspire backend. The API can list the name of each
                |                         | available backend using the function api.list_backend_types(). One of the
                |                         | listed names must be used.
                | backend_version (str)   | Backend version in the form X.Y.Z.
                | n_qubits (int)          | Number of qubits.
                | basis_gates (list[str]) | A list of basis gates to compile to.
                | gates (GateConfig)      | List of basis gates on the backend. Not used.
                | local (bool)            | Indicates whether the system is running locally or remotely. Not used.
                | simulator (bool)        | Specifies whether the backend is a simulator or a quantum system. Not used.
                | conditional (bool)      | Backend supports conditional operations.
                | open_pulse (bool)       | Backend supports open pulse. False.
                | memory (bool)           | Backend supports memory. True.
                | max_shots (int)         | Maximum number of shots supported.
                | max_experiments (int)   | Optional: Maximum number of experiments (circuits) per job.
        """
        super().__init__(configuration=(configuration or
                                        QuantumInspireBackend.DEFAULT_CONFIGURATION),
                         provider=provider)
        self.__backend: Dict[str, Any] = api.get_backend_type_by_name(self.name())
        self.__api: QuantumInspireAPI = api
예제 #2
0
    def __init__(self,
                 num_runs: int = 1024,
                 verbose: int = 0,
                 quantum_inspire_api: Optional[QuantumInspireAPI] = None,
                 backend_type: Optional[Union[int, str]] = None) -> None:
        """
        Initialize the Backend object.

        :param num_runs: Number of runs to collect statistics (default is 1024).
        :param verbose: Verbosity level, defaults to 0, which produces no extra output.
        :param quantum_inspire_api: Connection to QI platform, optional parameter.
        :param backend_type: Backend to use for execution.
            When no backend_type is provided, the default backend will be used.

        :raises AuthenticationError: When an authentication error occurs.
        """
        BasicEngine.__init__(self)
        self._flushed: bool = False
        """ Because engines are meant to be 'single use' by the way ProjectQ is designed,
        any additional gates received after a FlushGate triggers an exception. """
        self._clear: bool = True
        self._qasm: str = ""
        self._reset()
        self._verbose: int = verbose
        self._cqasm: str = str()
        self._measured_states: Dict[int, float] = {}
        self._measured_ids: List[int] = []
        self._allocation_map: List[Tuple[int, int]] = []
        self._max_qubit_id: int = -1
        self._quantum_inspire_result: Dict[str, Any] = {}
        if quantum_inspire_api is None:
            try:
                quantum_inspire_api = QuantumInspireAPI()
            except AuthenticationError as ex:
                raise AuthenticationError(
                    'Make sure you have saved your token credentials on disk or '
                    'provide a QuantumInspireAPI instance as parameter to QIBackend'
                ) from ex
        self._quantum_inspire_api: QuantumInspireAPI = quantum_inspire_api
        self._backend_type: Dict[
            str,
            Any] = self._quantum_inspire_api.get_backend_type(backend_type)
        if num_runs < 1 or num_runs > self._backend_type['max_number_of_shots']:
            raise ProjectQBackendError(
                f'Invalid number of runs (num_runs={num_runs})')
        self._num_runs: int = num_runs
        self._full_state_projection = not self._backend_type[
            "is_hardware_backend"]
        self._is_simulation_backend = not self._backend_type[
            "is_hardware_backend"]
        self._max_number_of_qubits: int = self._backend_type[
            "number_of_qubits"]
        self._one_qubit_gates: Tuple[Any, ...] = self._get_one_qubit_gates()
        self._two_qubit_gates: Tuple[Any, ...] = self._get_two_qubit_gates()
        self._three_qubit_gates: Tuple[Any,
                                       ...] = self._get_three_qubit_gates()
예제 #3
0
    def set_authentication(self, authentication: Optional[coreapi.auth.AuthBase] = None,
                           qi_url: str = QI_URL) -> None:
        """
        Initializes the API and sets the authentication for Quantum Inspire.

        Args:
            authentication: The authentication, can be one of the following coreapi authentications:
                            BasicAuthentication(email, password), HTTP authentication with valid email/password.
                            TokenAuthentication(token, scheme="token"), token authentication with a valid API-token.
                            When authentication is None, api will try to load a token from the default resource.
            qi_url: URL that points to quantum-inspire api. Default value: 'https://api.quantum-inspire.com'.
        """
        self._api = QuantumInspireAPI(qi_url, authentication)
    def set_authentication(self,
                           authentication: Optional[
                               coreapi.auth.AuthBase] = None,
                           qi_url: str = QI_URL,
                           project_name: Optional[str] = None) -> None:
        """
        Initializes the API and sets the authentication for Quantum Inspire.

        :param authentication: The authentication, can be one of the following coreapi authentications:

            * ``BasicAuthentication(email, password)``, HTTP authentication with valid email/password.
            * ``TokenAuthentication(token, scheme="token")``, token authentication with a valid API-token.

            When authentication is ``None``, the api will try to load a token from the default resource.

        :param qi_url: URL that points to quantum-inspire api. Default value: 'https://api.quantum-inspire.com'.
        :param project_name: The project name of the project that is used for executing the jobs.
        """
        self._api = QuantumInspireAPI(qi_url, authentication, project_name)
예제 #5
0
    def __init__(self, api: QuantumInspireAPI, provider: Any,
                 configuration: Optional[QasmBackendConfiguration] = None) -> None:
        """ Python implementation of a quantum simulator using Quantum Inspire API.

        :param api: The interface instance to the Quantum Inspire API.
        :param provider: Provider for this backend.
        :param configuration: The configuration of the Quantum Inspire backend. The
                configuration must implement the fields given by the QiSimulatorPy.DEFAULT_CONFIGURATION. All
                configuration fields are listed in the table below. The table rows with an asterisk specify fields which
                can have a custom value and are allowed to be changed according to the description column.

                =================== ============= =====================================================================
                Key                 Type          Description
                =================== ============= =====================================================================
                ``backend_name`` *  str           The name of the Quantum Inspire backend. The API can list the name of
                                                  each available backend using the function api.list_backend_types().
                                                  One of the listed names must be used.
                ``backend_version`` str           Backend version in the form X.Y.Z.
                ``n_qubits``        int           Number of qubits.
                ``basis_gates``     list[str]     A list of basis gates to compile to.
                ``gates``           GateConfig    List of basis gates on the backend. Not used.
                ``local``           bool          Indicates whether the system is running locally or remotely.
                ``simulator``       bool          Specifies whether the backend is a simulator or a quantum system.
                ``conditional``     bool          Backend supports conditional operations.
                ``open_pulse``      bool          Backend supports open pulse. False.
                ``memory``          bool          Backend supports memory. True.
                ``max_shots``       int           Maximum number of shots supported.
                ``max_experiments`` int           Optional: Maximum number of experiments (circuits) per job.
                ``coupling_map``    list[list]    Define the edges.
                =================== ============= =====================================================================
        """
        super().__init__(configuration=(configuration or
                                        QuantumInspireBackend.DEFAULT_CONFIGURATION),
                         provider=provider)
        self.__backend: Dict[str, Any] = api.get_backend_type_by_name(self.name())
        self.__api: QuantumInspireAPI = api
예제 #6
0
from src.qasm_error_introducer.error_introducer import introduce_error
from src.qasm_topology_mapper.mapping import map_to_topology
from src.quantum_phase_estimation.processing.classical_postprocessing import print_result, remove_degeneracy
from src.quantum_phase_estimation.plot_results import plot_results

import numpy as np

if __name__ == "__main__":
    QI_EMAIL = os.getenv('QI_EMAIL')
    QI_PASSWORD = os.getenv('QI_PASSWORD')
    QI_URL = os.getenv('API_URL', 'https://api.quantum-inspire.com/')

    authentication = get_authentication(qi_email=QI_EMAIL,
                                        qi_password=QI_PASSWORD,
                                        token=load_account())
    qi = QuantumInspireAPI(QI_URL, authentication, 'matrix')

    # variables
    phase = 0.08
    print(phase)
    unitary_qasm = f"QASM\nRz q[0], {-phase*2*np.pi}"
    unitary_matrix = np.array([[np.exp(phase * 1j * np.pi), 0],
                               [0, np.exp(-phase * 1j * np.pi)]])
    unitary = unitary_matrix
    custom_prepare = "prep_z q[0]\n X q[0]"
    desired_bit_accuracy = 5
    minimum_chance_of_success = 0.5
    mu = 0.5
    sigma = 0.5
    error_toggle = 0
    topology = None
예제 #7
0
    else:
        if QI_EMAIL is None or QI_PASSWORD is None:
            print('Enter email:')
            email = input()
            print('Enter password')
            password = getpass()
        else:
            email, password = QI_EMAIL, QI_PASSWORD
        return get_basic_authentication(email, password)


if __name__ == '__main__':

    # Remote Quantum-Inspire backend #
    authentication = get_authentication()
    qi = QuantumInspireAPI(r'https://api.quantum-inspire.com/', authentication)

    compiler_engines = restrictedgateset.get_engine_list(
        one_qubit_gates="any", two_qubit_gates=(CNOT, CZ, Toffoli))
    compiler_engines.extend([ResourceCounter()])
    qi_backend = QIBackend(quantum_inspire_api=qi)
    qi_engine = MainEngine(backend=qi_backend, engine_list=compiler_engines)

    # Run remote Grover search to find a n-bit solution
    result_qi = run_grover(qi_engine, 3, alternating_bits_oracle)
    print("\nResult from the remote Quantum-Inspire backend: {}".format(
        result_qi))

    # Local ProjectQ simulator backend #
    compiler_engines = restrictedgateset.get_engine_list(one_qubit_gates="any",
                                                         two_qubit_gates=(CNOT,
예제 #8
0
    def setup_tabs(self):
        _translate = QtCore.QCoreApplication.translate
        self.ibm_backends = None
        self.dwave_backends = None
        self.inspire_backends = None
        self.rigetti_backends = None
        self.tab_infos = []

        # backend1 = {'name': '9q-square-noisy-qvm', 'number_of_qubits': 9,
        #             'max_number_of_shots': 65536, 'max_experiments': 'unlimited',
        #             'description': 'v. 1.17', 'accuracy': 'not accurate',
        #             'remaining_jobs': 'unlimited'}
        # backend2 = {'name': '26q-qvm', 'number_of_qubits': 26,
        #             'max_number_of_shots': 65536, 'max_experiments': 'unlimited',
        #             'description': 'v. 1.17', 'accuracy': '0',
        #             'remaining_jobs': 'unlimited'}
        # self.rigetti_backends = [backend1, backend2]

        if self.__qiskit is not None:
            IBMQ.enable_account(self.__qiskit)
            ibm_provider = IBMQ.get_provider('ibm-q')
            self.ibm_backends = ibm_provider.backends()

        if self.__inspire is not None:
            enable_account(self.__inspire)
            QI.set_authentication()
            server_url = r'https://api.quantum-inspire.com'
            auth = get_token_authentication()
            self.qi = QuantumInspireAPI(server_url, auth)
            # backends = []
            # for back in self.qi.get_backend_types():
            #     current_backend = {'name': back.get('name'), 'number_of_qubits': back.get('number_of_qubits'),
            #                        'max_number_of_shots': back.get('max_number_of_shots'),
            #                        'max_experiments': back.get('max_number_of_simultaneous_jobs'),
            #                        'description': back.get('description'), 'accuracy': 'not measured',
            #                        'remaining_jobs': back.get('max_number_of_simultaneous_jobs')}
            #     backends.append(current_backend)
            self.inspire_backends = self.qi.get_backend_types()

        if self.ibm_backends is not None:
            for backend in self.ibm_backends:
                self.tab_infos.append(ComputerInfoTab(backend))

        # if self.inspire_backends is not None:
        #     for backend in self.inspire_backends:
        #         # server = r'https://api.quantum-inspire.com'
        #         # email = '*****@*****.**'
        #         # password = '******'
        #         # auth = HTTPBasicAuth(email, password)
        #         #
        #         # result = requests.get(f'{server}/projects', auth=auth)
        #         #
        #         # print(f'result status: {result.status_code}')
        #         # print(result.json())
        #         # server = r'https://api.quantum-inspire.com'
        #         # auth = coreapi.auth.TokenAuthentication(self.__inspire)
        #         # client = coreapi.Client(auth=auth)
        #         # content = client.get(f'{server}/backendtypes/1/calibration')
        #         # print(content)
        #         # action = ['projects', 'list']
        #         # result = client.action(schema, action)
        #         # print(result)
        #         # server = r'https://api.quantum-inspire.com'
        #         # email = '*****@*****.**'
        #         # password = '******'
        #         # auth = HTTPBasicAuth(email, password)
        #         # contents = urllib.request.urlopen(r'https://api.quantum-inspire.com/backendtypes/1/calibration/',).read()
        #         self.tab_infos.append(ComputerInfoTab(backend._QuantumInspireBackend__backend))

        if self.inspire_backends is not None:
            for backend in self.inspire_backends:
                self.tab_infos.append(ComputerInfoTab(backend))

        if self.rigetti_backends is not None:
            for backend in self.rigetti_backends:
                self.tab_infos.append(ComputerInfoTab(backend))

        self.tab_widget = QtWidgets.QTabWidget(self.central_widget)
        self.tab_widget.setObjectName("tabWidget")

        self.tab_widget.addTab(self.test_info_tab.tab, "")
        self.tab_widget.setTabText(
            self.tab_widget.indexOf(self.test_info_tab.tab),
            _translate('main_window', self.test_info_tab.name))

        self.tab_widget.addTab(self.results_info_tab.tab, "")
        self.tab_widget.setTabText(
            self.tab_widget.indexOf(self.results_info_tab.tab),
            _translate('main_window', self.results_info_tab.name))

        for tab_info in self.tab_infos:
            self.tab_widget.addTab(tab_info.tab, "")
            self.tab_widget.setTabText(
                self.tab_widget.indexOf(tab_info.tab),
                _translate("main_window", tab_info.tab.objectName()))

        self.gridLayout_2.addWidget(self.tab_widget, 0, 0, 1, 1)
        self.tab_widget.setCurrentIndex(0)
예제 #9
0
class UiMainWindow(object):
    __result = None

    def __init__(self, main_window_local):
        grover_circuit_constructor = GroverCircuit()
        shor_circuit_constructor = ShorCircuit()
        tokens = list(self.get_config().values())
        self.__qiskit = tokens[0]
        self.__dwave = tokens[1]
        self.__inspire = tokens[2]

        self.registration_dialog = RegistrationDialogWindow(init=tokens)

        self.ibm_backends = None
        self.dwave_backends = None
        self.inspire_backends = None

        self.window = main_window_local
        self.central_widget = QtWidgets.QWidget(main_window_local)
        self.central_widget.setObjectName("central_widget")
        self.repo = DataRepository()
        saved_results = self.repo.get_all()

        self.thread_pool = QThreadPool()
        self.thread_pool.setMaxThreadCount(8)

        self.inner_thread_pool = QThreadPool()
        self.inner_thread_pool.setMaxThreadCount(8)

        self.drawer = PlotDrawer()
        self.writer = FileWriter()
        self.tab_infos = []
        self.default_tests = []

        # quantumRegister = q.QuantumRegister(2)
        # classicalRegister = q.ClassicalRegister(2)
        # circuit = q.QuantumCircuit(quantumRegister, classicalRegister)
        # circuit.h(quantumRegister[0])
        # circuit.cx(quantumRegister[0], quantumRegister[1])
        # circuit.measure(quantumRegister, classicalRegister)
        # self.default_tests.append(QuantumTest("BellState", circuit, 1024))

        shor_answer = 4
        grover_2_answer = 3
        grover_3_answer = 6
        grover_4_answer = 2
        grover_5_answer = 21

        circuit3 = q.QuantumCircuit(5, 5)
        shor_circuit_constructor.add_circuit(circuit3)
        self.default_tests.append(
            QuantumTest('Shor_3_5', circuit3, 1024, shor_answer))

        circuit4 = q.QuantumCircuit(2, 2)
        grover_circuit_constructor.set_number(grover_2_answer)
        grover_circuit_constructor.add_circuit(circuit4)
        self.default_tests.append(
            QuantumTest('Grover_2_bits', circuit4, 1024, grover_2_answer))

        circuit_new = q.QuantumCircuit(3, 3)
        grover_circuit_constructor.set_number(grover_3_answer)
        grover_circuit_constructor.add_circuit(circuit_new)
        self.default_tests.append(
            QuantumTest('Grover_3_bits', circuit_new, 1024, grover_3_answer))

        circuit5 = q.QuantumCircuit(4, 4)
        grover_circuit_constructor.set_number(grover_4_answer)
        grover_circuit_constructor.add_circuit(circuit5)
        self.default_tests.append(
            QuantumTest('Grover_4_bits', circuit5, 1024, grover_4_answer))

        qubits = q.QuantumRegister(5)
        bits = q.ClassicalRegister(5)
        # controls = list()
        # controls.append(qubits[0])
        # controls.append(qubits[1])
        # controls.append(qubits[2])
        circuit6 = q.QuantumCircuit(qubits, bits)
        grover_circuit_constructor.set_number(grover_5_answer)
        grover_circuit_constructor.add_circuit(circuit6)

        self.default_tests.append(
            QuantumTest('Grover_5_bits', circuit6, 1024, grover_5_answer))

        self.test_info_tab = TestInfoTab(self.default_tests, self.drawer)
        self.results_info_tab = TestResultTab()

        self.setup_ui(main_window_local)

        self.setup_tabs()

        self.dialog = TestCreationDialogWindow(self.tab_infos,
                                               self.default_tests)
        self.dialog.setWindowTitle('Create test')

        for res in saved_results:
            self.add_result_from_db(res)

    def get_config(self):
        keys = ['qiskit', 'dwave', 'quantum-inspire']
        tokens = dict.fromkeys(keys)
        try:
            with open('main\\configuration\\config.ini') as config_file:
                lines = config_file.readlines()
                for line in lines:
                    api_name = line.strip().split('=')[0]
                    if api_name in keys:
                        tokens[api_name] = line.strip().split('=')[1]
        except OSError:
            print('Config file not found')
        except:
            print(sys.exc_info()[0])
            print('Corrupted file')
        return tokens

    def open_qasm_circuit(self):
        files_filter = 'QASM files (*.qasm);;All files (*.*)'
        name = QtWidgets.QFileDialog.getOpenFileName(self.window,
                                                     'Open File',
                                                     filter=files_filter)
        extension = os.path.splitext(name[0])[1]
        if extension == '.qasm':
            try:
                new_circuit = q.QuantumCircuit().from_qasm_file(name[0])
                new_test = QuantumTest(
                    os.path.basename(os.path.splitext(name[0])[0]),
                    new_circuit, 1024)
                self.default_tests.append(new_test)
                self.test_info_tab.add_new_test(new_test)
                self.dialog.add_new_test(new_test)
            except QasmError:
                msg = QtWidgets.QMessageBox()
                msg.setIcon(QtWidgets.QMessageBox.Critical)
                msg.setText('File %s is corrupted!' %
                            os.path.splitext(name[0])[0])
                msg.setWindowTitle('Error!')
                msg.exec_()

    def delete_results(self):
        indexes = self.results_info_tab.table_widget.selectionModel(
        ).selectedRows()
        for i in range(0, len(indexes)):
            self.results_info_tab.table_widget.removeRow(indexes[i].row())
        self.results_info_tab.table_widget.resizeColumnsToContents()

    def open_results(self):  # TODO
        files_filter = 'CSV files (*.csv);;All files (*.*)'
        name = QtWidgets.QFileDialog.getOpenFileName(self.window,
                                                     'Open File',
                                                     filter=files_filter)
        extension = os.path.splitext(name[0])[1]
        if extension == '.csv':
            try:
                with open(name[0], newline='') as csvfile:
                    csv_reader = csv.reader(csvfile, delimiter=';')
                    headers = next(csv_reader, None)
                    if headers != [
                            'Test_name', 'Result', 'Backend_name',
                            'Job_status', 'Date', 'Shots_taken', 'Time_taken',
                            'Accuracy', 'Counts'
                    ]:
                        raise RuntimeError('Incorrect file format!')
                    for row in csv_reader:
                        if len(row) == 0:
                            continue
                        self.results_info_tab.table_widget.insertRow(
                            self.results_info_tab.table_widget.rowCount())
                        dict_data = dict.fromkeys(headers)
                        test = next(
                            (x
                             for x in self.default_tests if x.name == row[0]),
                            QuantumTest(row[0], None, row[5], row[1]))
                        if test not in self.default_tests:
                            self.default_tests.append(test)

                        dict_data[headers[0]] = row[0]
                        self.results_info_tab.table_widget.setItem(
                            self.results_info_tab.table_widget.rowCount() - 1,
                            0, MyTableWidgetItem(str(row[0])))
                        for j in range(2, 8):
                            dict_data[headers[j]] = row[j]
                            self.results_info_tab.table_widget.setItem(
                                self.results_info_tab.table_widget.rowCount() -
                                1, j - 1, MyTableWidgetItem(str(row[j])))
                        dict_data[headers[8]] = row[8]
                        test.results.append(dict_data)
                    self.results_info_tab.table_widget.resizeColumnsToContents(
                    )

            except:
                msg = QtWidgets.QMessageBox()
                msg.setIcon(QtWidgets.QMessageBox.Critical)
                msg.setText("Oops! %s occurred." % sys.exc_info()[0])
                msg.setWindowTitle('Error!')
                msg.exec_()

    def save_results(self):
        files_filter = 'CSV files (*.csv);;All files (*.*)'
        name = QtWidgets.QFileDialog.getSaveFileName(self.window,
                                                     'Save File',
                                                     filter=files_filter)
        if all(name):
            func = self.writer.write_result(
                os.path.basename(os.path.splitext(name[0])[0]),
                self.default_tests)
            func()

    def save_qasm_circuit(self):
        files_filter = 'QASM files (*.qasm);;All files (*.*)'
        name = QtWidgets.QFileDialog.getSaveFileName(self.window,
                                                     'Save File',
                                                     filter=files_filter)
        if all(name):
            file = open(name[0], 'w')
            text = self.test_info_tab.get_selected_test().circuit.qasm()
            file.write(text)
            file.close()

    def background_calc(self, cur_circuit, cur_computer):
        job = q.execute(cur_circuit, backend=cur_computer)
        job_monitor(job)
        self.__result = job.result()
        return job.result()

    def test_and_print(self, cur_test, computers):
        print('Starting test and print')
        worker = Worker(self.test_selected_computers, cur_test, computers)
        print('Pool start')
        self.thread_pool.start(worker)

    def print_s(self):
        print('s')

    def add_result_from_db(self, result: Result):
        self.results_info_tab.table_widget.insertRow(
            self.results_info_tab.table_widget.rowCount())
        res_list = list()
        res_list.append(result.test_name)
        res_list.append(result.backend_name)
        res_list.append(result.job_status)
        res_list.append(result.date)
        res_list.append(result.shots)
        res_list.append(result.time)
        res_list.append(result.accuracy)
        for j in range(0, 7):
            self.results_info_tab.table_widget.setItem(
                self.results_info_tab.table_widget.rowCount() - 1, j,
                MyTableWidgetItem(str(res_list[j])))
        self.results_info_tab.table_widget.resizeColumnsToContents()

    def add_new_result(self, result):
        self.results_info_tab.table_widget.insertRow(
            self.results_info_tab.table_widget.rowCount())
        res_list = list(result.values())
        result_value = result['Result']
        res_list.remove(result_value)
        for j in range(0, 7):
            self.results_info_tab.table_widget.setItem(
                self.results_info_tab.table_widget.rowCount() - 1, j,
                MyTableWidgetItem(str(res_list[j])))
        self.results_info_tab.table_widget.resizeColumnsToContents()

    def test_selected_computers(self, cur_test, computers):
        fields = [
            'Test_name', 'Backend_name', 'Job_status', 'Date', 'Shots_taken',
            'Time_taken', 'Counts'
        ]
        print('start test_selected')
        print(str(self))
        print(str(cur_test))
        print(str(computers))
        tabs = []
        test = next(x for x in self.default_tests if x.name == cur_test)
        backend = None
        for computer in computers:
            try:
                backend = next(x for x in self.ibm_backends
                               if str(x) == computer)
            except StopIteration:
                for back in self.inspire_backends:
                    if computer == back['name']:
                        backend = back
            if backend is None:
                raise RuntimeError('Unknown backend')
            tab = next(x for x in self.tab_infos if x.name == computer)
            tabs.append(tab)
            if computer in [
                    'QX single-node simulator',
                    'QX single-node simulator SurfSara', 'Spin-2', 'Starmon-5'
            ]:
                worker = Worker2(test.run_inspire, backend, self.qi)
                print('Start inner pool')
                self.inner_thread_pool.start(worker)
            else:
                worker = Worker2(test.run_ibm, backend)
                print('Start inner pool')
                self.inner_thread_pool.start(worker)
        self.inner_thread_pool.waitForDone()

        for i in range(0, len(tabs)):
            self.drawer.plot_results2(tabs[i], test.not_drawn_res[i]['Counts'])
        print('Finished after testing work')
        for result in test.not_drawn_res:
            self.add_new_result(result)
            self.repo.insert_result(Result.create_result_from_dict(result))
        test.not_drawn_res.clear()

    def enter_keys(self):
        self.registration_dialog.show_dialog()
        if self.registration_dialog.result() == QtWidgets.QDialog.Accepted:
            qiskit_token, dwave_token, inspire_token = self.registration_dialog.provide_api_tokens(
            )
            is_update_necessary = False
            if self.__qiskit != qiskit_token:
                self.__qiskit = qiskit_token
                is_update_necessary = True
            if self.__dwave != dwave_token:
                self.__dwave = dwave_token
                is_update_necessary = True
            if self.__inspire != inspire_token:
                self.__inspire = inspire_token
                is_update_necessary = True

            if is_update_necessary:
                self.setup_tabs()
                self.dialog = TestCreationDialogWindow(self.tab_infos,
                                                       self.default_tests)

    def choose_test(self):
        self.dialog.show_dialog()
        if self.dialog.result() == QtWidgets.QDialog.Accepted:
            test = self.dialog.get_selected_test()
            computers = self.dialog.get_selected_computers()
            if computers:
                self.test_and_print(test, computers)

        elif self.dialog.result() == QtWidgets.QDialog.Rejected:
            self.status_label.setText('Rejected')

    def show_status(self, message):
        def show():
            self.status_label.setText(message)

        return show

    def setup_ui(self, main_window_local):
        main_window_local.setObjectName("main_window")
        main_window_local.resize(1024, 768)

        self.gridLayout_2 = QtWidgets.QGridLayout(self.central_widget)
        self.gridLayout_2.setObjectName("gridLayout_2")

        main_window_local.setCentralWidget(self.central_widget)
        self.menubar = QtWidgets.QMenuBar(main_window_local)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 766, 21))
        self.menubar.setObjectName("menubar")

        self.statusbar = QtWidgets.QStatusBar(main_window_local)
        self.statusbar.setObjectName("statusbar")
        main_window_local.setStatusBar(self.statusbar)

        self.menu_file = QtWidgets.QMenu(self.menubar)
        self.menu_file.setObjectName("menu_file")
        self.menu_service = QtWidgets.QMenu(self.menubar)
        self.menu_service.setObjectName("menu_service")
        self.menu_help = QtWidgets.QMenu(self.menubar)
        self.menu_help.setObjectName("menu_help")

        self.menu_edit = QtWidgets.QMenu(self.menubar)
        self.menu_edit.setObjectName("menu_edit")
        self.menu_edit.setStatusTip('Editing')

        main_window_local.setMenuBar(self.menubar)

        self.status_label = QtWidgets.QLabel(self.statusbar)
        self.status_label.setGeometry(5, -5, 500, 20)
        self.status_label.setObjectName('status_label')
        self.status_label.setText('')

        self.action_about = QtWidgets.QAction(main_window_local)
        self.action_about.setObjectName("action_about")
        self.action_about.setStatusTip('Подробнее о программе')

        self.action_registration = QtWidgets.QAction(main_window_local)
        self.action_registration.setObjectName('action_registration')
        self.action_registration.setStatusTip('Ввести ключи регистрации')
        self.action_registration.triggered.connect(self.enter_keys)

        self.action_new = QtWidgets.QAction(main_window_local)
        self.action_new.setObjectName("action_new")
        self.action_new.triggered.connect(self.choose_test)
        self.action_new.setStatusTip('Создать новый файл')

        self.test_info_tab.button_import.clicked.connect(
            self.open_qasm_circuit)
        self.test_info_tab.button_export.clicked.connect(
            self.save_qasm_circuit)

        self.results_info_tab.button_load.clicked.connect(self.open_results)
        self.results_info_tab.button_delete.clicked.connect(
            self.delete_results)

        self.action_open = QtWidgets.QAction(main_window_local)
        self.action_open.setObjectName("action_open")
        self.action_open.setStatusTip('Открыть файл результатов')

        self.action_save = QtWidgets.QAction(main_window_local)
        self.action_save.setObjectName("action_save")
        self.action_save.setStatusTip('Сохранить результаты')

        self.action_save_as = QtWidgets.QAction(main_window_local)
        self.action_save_as.setObjectName("action_save_as")
        self.action_save_as.setStatusTip('Сохранить результаты в новый файл')
        self.action_save_as.triggered.connect(self.save_results)

        self.action_close = QtWidgets.QAction(main_window_local)
        self.action_close.setObjectName("action_close")
        self.action_close.setStatusTip('Выйти из программы')

        self.action_update = QtWidgets.QAction(main_window_local)
        self.action_update.setObjectName("action_update")
        self.action_update.triggered.connect(self.clear_plots)
        self.action_update.setStatusTip('Очистить графики')

        self.action_test = QtWidgets.QAction(main_window_local)
        self.action_test.setObjectName("action_test")
        self.action_test.setStatusTip('Протестировать компьютеры')

        self.menu_file.addAction(self.action_new)
        self.menu_file.addAction(self.action_open)
        self.menu_file.addSeparator()
        self.menu_file.addAction(self.action_save)
        self.menu_file.addAction(self.action_save_as)
        self.menu_file.addSeparator()
        self.menu_file.addAction(self.action_close)
        self.menu_service.addAction(self.action_update)
        self.menu_service.addAction(self.action_test)
        self.menu_help.addAction(self.action_about)
        self.menu_help.addSeparator()
        self.menu_help.addAction(self.action_registration)
        self.menubar.addAction(self.menu_file.menuAction())
        self.menubar.addAction(self.menu_edit.menuAction())
        self.menubar.addAction(self.menu_service.menuAction())
        self.menubar.addAction(self.menu_help.menuAction())
        self.retranslate_ui(main_window_local)

        QtCore.QMetaObject.connectSlotsByName(main_window_local)

    def plot_result(self, cur_circuit, cur_result):
        def plot():
            for tab_info in self.tab_infos:
                axes = tab_info.figure.add_subplot(111)
                axes.clear()
                plot_histogram(self.__result.get_counts(cur_circuit), ax=axes)
                tab_info.canvas.draw()

        return plot

    def clear_plots(self):
        for tab_info in self.tab_infos:
            axes = tab_info.figure.add_subplot(111)
            axes.clear()
            tab_info.canvas.draw()

    def plot_circuit(self, cur_circuit):
        for tab_info in self.tab_infos:
            axes = tab_info.figure.add_subplot(111)
            axes.clear()
            cur_circuit.draw(output='mpl', ax=axes)
            tab_info.canvas.draw()

    def retranslate_ui(self, main_window_local):
        _translate = QtCore.QCoreApplication.translate
        main_window_local.setWindowTitle(
            _translate("main_window", "QuantumTester 0.0.1 x64"))
        self.menu_file.setTitle(_translate("main_window", "Файл"))
        self.menu_service.setTitle(_translate("main_window", "Сервис"))
        self.menu_help.setTitle(_translate("main_window", "Помощь"))
        self.menu_edit.setTitle(_translate("main_window", "Правка"))
        self.action_about.setText(_translate("main_window", "О программе..."))
        self.action_registration.setText(
            _translate("main_window", "Регистрация"))
        self.action_new.setText(_translate("main_window", "Новый"))
        self.action_new.setShortcut(_translate("main_window", "Ctrl+N"))
        self.action_open.setText(_translate("main_window", "Открыть"))
        self.action_open.setShortcut(_translate("main_window", "Ctrl+O"))
        self.action_save.setText(_translate("main_window", "Сохранить"))
        self.action_save.setShortcut(_translate("main_window", "Ctrl+S"))
        self.action_save_as.setText(
            _translate("main_window", "Сохранить как..."))
        self.action_close.setText(_translate("main_window", "Закрыть"))
        self.action_update.setText(_translate("main_window", "Обновить"))
        self.action_test.setText(_translate("main_window", "Протестировать"))

    def setup_tabs(self):
        _translate = QtCore.QCoreApplication.translate
        self.ibm_backends = None
        self.dwave_backends = None
        self.inspire_backends = None
        self.rigetti_backends = None
        self.tab_infos = []

        # backend1 = {'name': '9q-square-noisy-qvm', 'number_of_qubits': 9,
        #             'max_number_of_shots': 65536, 'max_experiments': 'unlimited',
        #             'description': 'v. 1.17', 'accuracy': 'not accurate',
        #             'remaining_jobs': 'unlimited'}
        # backend2 = {'name': '26q-qvm', 'number_of_qubits': 26,
        #             'max_number_of_shots': 65536, 'max_experiments': 'unlimited',
        #             'description': 'v. 1.17', 'accuracy': '0',
        #             'remaining_jobs': 'unlimited'}
        # self.rigetti_backends = [backend1, backend2]

        if self.__qiskit is not None:
            IBMQ.enable_account(self.__qiskit)
            ibm_provider = IBMQ.get_provider('ibm-q')
            self.ibm_backends = ibm_provider.backends()

        if self.__inspire is not None:
            enable_account(self.__inspire)
            QI.set_authentication()
            server_url = r'https://api.quantum-inspire.com'
            auth = get_token_authentication()
            self.qi = QuantumInspireAPI(server_url, auth)
            # backends = []
            # for back in self.qi.get_backend_types():
            #     current_backend = {'name': back.get('name'), 'number_of_qubits': back.get('number_of_qubits'),
            #                        'max_number_of_shots': back.get('max_number_of_shots'),
            #                        'max_experiments': back.get('max_number_of_simultaneous_jobs'),
            #                        'description': back.get('description'), 'accuracy': 'not measured',
            #                        'remaining_jobs': back.get('max_number_of_simultaneous_jobs')}
            #     backends.append(current_backend)
            self.inspire_backends = self.qi.get_backend_types()

        if self.ibm_backends is not None:
            for backend in self.ibm_backends:
                self.tab_infos.append(ComputerInfoTab(backend))

        # if self.inspire_backends is not None:
        #     for backend in self.inspire_backends:
        #         # server = r'https://api.quantum-inspire.com'
        #         # email = '*****@*****.**'
        #         # password = '******'
        #         # auth = HTTPBasicAuth(email, password)
        #         #
        #         # result = requests.get(f'{server}/projects', auth=auth)
        #         #
        #         # print(f'result status: {result.status_code}')
        #         # print(result.json())
        #         # server = r'https://api.quantum-inspire.com'
        #         # auth = coreapi.auth.TokenAuthentication(self.__inspire)
        #         # client = coreapi.Client(auth=auth)
        #         # content = client.get(f'{server}/backendtypes/1/calibration')
        #         # print(content)
        #         # action = ['projects', 'list']
        #         # result = client.action(schema, action)
        #         # print(result)
        #         # server = r'https://api.quantum-inspire.com'
        #         # email = '*****@*****.**'
        #         # password = '******'
        #         # auth = HTTPBasicAuth(email, password)
        #         # contents = urllib.request.urlopen(r'https://api.quantum-inspire.com/backendtypes/1/calibration/',).read()
        #         self.tab_infos.append(ComputerInfoTab(backend._QuantumInspireBackend__backend))

        if self.inspire_backends is not None:
            for backend in self.inspire_backends:
                self.tab_infos.append(ComputerInfoTab(backend))

        if self.rigetti_backends is not None:
            for backend in self.rigetti_backends:
                self.tab_infos.append(ComputerInfoTab(backend))

        self.tab_widget = QtWidgets.QTabWidget(self.central_widget)
        self.tab_widget.setObjectName("tabWidget")

        self.tab_widget.addTab(self.test_info_tab.tab, "")
        self.tab_widget.setTabText(
            self.tab_widget.indexOf(self.test_info_tab.tab),
            _translate('main_window', self.test_info_tab.name))

        self.tab_widget.addTab(self.results_info_tab.tab, "")
        self.tab_widget.setTabText(
            self.tab_widget.indexOf(self.results_info_tab.tab),
            _translate('main_window', self.results_info_tab.name))

        for tab_info in self.tab_infos:
            self.tab_widget.addTab(tab_info.tab, "")
            self.tab_widget.setTabText(
                self.tab_widget.indexOf(tab_info.tab),
                _translate("main_window", tab_info.tab.objectName()))

        self.gridLayout_2.addWidget(self.tab_widget, 0, 0, 1, 1)
        self.tab_widget.setCurrentIndex(0)
    Args:
        eng (MainEngine): Main compiler engine the algorithm is being run on.
        qubits (Qureg): n-qubit quantum register Grover search is run on.
        output (Qubit): Output qubit to flip in order to mark the solution.
    """
    with Compute(eng):
        All(X) | qubits[1::2]
    with Control(eng, qubits):
        X | output
    Uncompute(eng)


# Remote Quantum-Inspire backend
authentication = get_authentication()
qi = QuantumInspireAPI(QI_URL, authentication)
qi_backend = QIBackend(quantum_inspire_api=qi)

compiler_engines = restrictedgateset.get_engine_list(
    one_qubit_gates=qi_backend.one_qubit_gates,
    two_qubit_gates=qi_backend.two_qubit_gates,
    other_gates=qi_backend.three_qubit_gates)
compiler_engines.extend([ResourceCounter()])
qi_engine = MainEngine(backend=qi_backend, engine_list=compiler_engines)

# Run remote Grover search to find a n-bit solution
result_qi = run_grover(qi_engine, 3, alternating_bits_oracle)
print("\nResult from the remote Quantum-Inspire backend: {}".format(result_qi))

# Local ProjectQ simulator backend
compiler_engines = restrictedgateset.get_engine_list(one_qubit_gates="any",
예제 #11
0
from src.grover.run import *
from quantuminspire.credentials import enable_account
from quantuminspire.api import QuantumInspireAPI
import time

start_login = time.time()
enable_account("58957ea5a48a801eb5af6adcae7776126c122c9d")
qi = QuantumInspireAPI()
print("Logged in to QI account ({} seconds)".format(str(time.time() - start_login)[:5]))\

backend = qi.get_backend_type_by_name('QX single-node simulator')

shot_count = 500

# whether to apply optimization to our generated QASM
# performance improvement of ~20-50%
apply_optimization_to_qasm = True

# MODES:
#   - normal: use toffoli gates and ancillary qubits for max speed
#   - no toffoli: same as normal, but replace toffoli gates for 2-gate equivalent circuits. uses ancillary qubits.
#   - crot: no ancillary qubits or toffoli gates, but scales with 3^n gates for n bits
#   - fancy cnot: no ancillary qubits or toffoli gates, scales 2^n
mode = "normal"

# Search example
search_targets = [
    "0110"[::-1],
    "1010"[::-1],
]
예제 #12
0
    else:
        if QI_EMAIL is None or QI_PASSWORD is None:
            print('Enter email:')
            email = input()
            print('Enter password')
            password = getpass()
        else:
            email, password = QI_EMAIL, QI_PASSWORD
        return get_basic_authentication(email, password)


if __name__ == '__main__':

    name = 'TestProjectQ'
    authentication = get_authentication()
    qi_api = QuantumInspireAPI(QI_URL, authentication, project_name=name)

    compiler_engines = restrictedgateset.get_engine_list(one_qubit_gates="any", two_qubit_gates=(CNOT,))
    compiler_engines.extend([ResourceCounter()])

    qi_backend = QIBackend(quantum_inspire_api=qi_api)
    engine = MainEngine(backend=qi_backend, engine_list=compiler_engines)

    qubits = engine.allocate_qureg(2)
    q1 = qubits[0]
    q2 = qubits[1]

    H | q1
    CNOT | (q1, q2)
    All(Measure) | qubits
def testErrorCorrection():
    server_url = r'https://api.quantum-inspire.com'
    print('Enter mail address')
    email = input()

    print('Enter password')
    password = getpass()
    auth = (email, password)

    qi = QuantumInspireAPI(server_url, auth)
    backend = qi.get_backend_type_by_name('QX single-node simulator')

    inputValues = [
        {
            'theta': 0,
            'phi': 0
        },  #|0>
        {
            'theta': np.pi,
            'phi': np.pi
        },  #|1>
        {
            'theta': np.pi / 2,
            'phi': np.pi / 2
        }  #|+>
    ]

    for inputValue in inputValues:
        print("Using input value " + str(inputValue))
        allErrorIdxs = []
        for nrOfErrors in range(2):
            allErrorIdxs += itertools.combinations(range(nbqubits), nrOfErrors)
        # iterate over all possible combinations of errors

        for errorIdxs in allErrorIdxs:
            inputStateComplex = _stateForInput(inputValue['theta'],
                                               inputValue['phi'])

            print("Introducing errors, indices " + str(errorIdxs))
            QASM = createProgram(inputValue, errorIdxs)

            prob_0 = 0
            prob_1 = 0

            if 1:  # Wavefunction
                result = qi.execute_qasm(QASM,
                                         backend_type=backend,
                                         number_of_shots=1)
            else:  # Do measurements
                result = qi.execute_qasm(QASM,
                                         backend_type=backend,
                                         number_of_shots=10)

            result = result['histogram']
            for key, value in zip(result.keys(), result.values()):
                if (int(key) % 2) == 0:  #Number is odd
                    prob_0 += value
                else:
                    prob_1 += value

            if abs(prob_0 - abs(inputStateComplex[0])**2) < 0.0001 and abs(
                    prob_1 - abs(inputStateComplex[1])**2) < 0.0001:
                print("Error correction works")
            else:
                print("Error correction fails")
예제 #14
0
from src.quantum_phase_estimation.util_functions import error_estimate
from src.quantum_phase_estimation.util_functions import decimal_to_binary_fracion, find_max_value_keys, to_array
from src.quantum_phase_estimation.processing.plotting import heatmap, graph
from quantuminspire.credentials import load_account

if __name__ == "__main__":
    QI_EMAIL = os.getenv('QI_EMAIL')
    QI_PASSWORD = os.getenv('QI_PASSWORD')
    QI_URL = os.getenv('API_URL', 'https://api.quantum-inspire.com/')

    tokens = [load_account()]
    qis = list(
        map(
            lambda x: QuantumInspireAPI(
                QI_URL,
                get_authentication(
                    qi_email=QI_EMAIL, qi_password=QI_PASSWORD, token=x),
                'Quantum Phase Estimation matrix'), tokens))

    # variables
    desired_bit_accuracy = 6
    minimum_chance_of_success = 0.5
    mu = 0
    sigma = 0.05
    use_error_model = False
    use_multiple = True
    if False:
        topology = [['0', '1'], ['0', '3'], ['1', '2'], ['1', '4'], ['2', '5'],
                    ['3', '4'], ['3', '6'], ['4', '5'], ['4', '7'], ['5', '8'],
                    ['6', '7'], ['7', '8'], ['8', '9'], ['9', '10'],
                    ['10', '11'], ['11', '12'], ['12', '13'], ['13', '14']]
예제 #15
0
def get_api_session():
    return QuantumInspireAPI(QI_URL, get_authentication())