Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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)
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(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", two_qubit_gates=(CNOT, CZ))
compiler_engines.append(ResourceCounter())
local_engine = MainEngine(Simulator(), compiler_engines)
def __init__(self, *args: Any, **kwargs: Any) -> None:
super().__init__(*args, **kwargs)
self._backends: List[QuantumInspireBackend] = []
self._api: Optional[QuantumInspireAPI] = None
"""
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._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
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()
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)
qi_backend = QIBackend(quantum_inspire_api=qi_api)
compiler_engines = restrictedgateset.get_engine_list(one_qubit_gates=qi_backend.one_qubit_gates,
two_qubit_gates=qi_backend.two_qubit_gates)
compiler_engines.extend([ResourceCounter()])
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
engine.flush()