def test_save_account(self): """Test saving one account.""" with custom_qiskitrc(), mock_ibmq_provider(): IBMQ.save_account('QISKITRC_TOKEN', url=QE_URL, proxies=PROXIES) # Compare the session accounts with the ones stored in file. stored_accounts = read_credentials_from_qiskitrc() self.assertEqual(len(stored_accounts.keys()), 1)
def login(cls, token: str = "", remember: bool = False, **kwargs) -> bool: if not token: return cls._try_login_saved_account() else: QiskitIBMQ.enable_account(token, **kwargs) if remember: QiskitIBMQ.save_account(token, **kwargs)
def test_delete_accounts(self): """Test deleting an account from disk.""" with custom_qiskitrc(), mock_ibmq_provider(): IBMQ.save_account('QISKITRC_TOKEN') self.assertEqual(len(read_credentials_from_qiskitrc()), 1) IBMQ._accounts.clear() IBMQ.delete_accounts(token='QISKITRC_TOKEN') self.assertEqual(len(read_credentials_from_qiskitrc()), 0)
def load_account_oneshot(): try: IBMQ.load_account() except IBMQAccountError as e: print("Couldn't load IBMQ account: {}".format(e)) print("Try again, loading API key from file...") with open(IBMQ_TOKEN_FILENAME, 'r') as file: IBMQ.save_account(file.read()) IBMQ.load_account()
def test_delete_all_accounts(self): """Test deleting all accounts from disk.""" with custom_qiskitrc(), mock_ibmq_provider(): IBMQ.save_account('QISKITRC_TOKEN') IBMQ.save_account('QISKITRC_TOKEN', url=IBMQ_TEMPLATE.format('a', 'b', 'c')) self.assertEqual(len(read_credentials_from_qiskitrc()), 2) IBMQ.delete_accounts() self.assertEqual(len(IBMQ._accounts), 0) self.assertEqual(len(read_credentials_from_qiskitrc()), 0)
def test_save_duplicate_accounts(self): """Test saving the same credentials twice.""" with custom_qiskitrc(), mock_ibmq_provider(): IBMQ.save_account('QISKITRC_TOKEN') with self.assertWarns(UserWarning) as context_manager: IBMQ.save_account('QISKITRC_TOKEN') self.assertIn('Set overwrite', str(context_manager.warning)) # Compare the session accounts with the ones stored in file. stored_accounts = read_credentials_from_qiskitrc() self.assertEqual(len(stored_accounts), 1)
def test_save_multiple_accounts(self): """Test saving several accounts, combining QX and IBMQ""" with custom_qiskitrc(), mock_ibmq_provider(): IBMQ.save_account('QISKITRC_TOKEN') IBMQ.save_account('QISKITRC_TOKEN', url=IBMQ_TEMPLATE.format('a', 'b', 'c')) IBMQ.save_account('QISKITRC_TOKEN', IBMQ_TEMPLATE.format('a', 'b', 'X')) # Compare the session accounts with the ones stored in file. stored_accounts = read_credentials_from_qiskitrc() self.assertEqual(len(stored_accounts), 3) for account_name, provider in IBMQ._accounts.items(): self.assertEqual(provider.credentials, stored_accounts[account_name])