Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add cirq specific environment variables to ionq #6216

Merged
merged 6 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions cirq-ionq/cirq_ionq/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,12 @@ def __init__(
This is actually an EnvironmentError which is equal to an OSError.
"""
self.remote_host = (
remote_host or os.getenv('IONQ_REMOTE_HOST') or f'https://api.ionq.co/{api_version}'
remote_host
or os.getenv('CIRQ_IONQ_REMOTE_HOST')
or os.getenv('IONQ_REMOTE_HOST')
or f'https://api.ionq.co/{api_version}'
)
self.api_key = api_key or os.getenv('IONQ_API_KEY')
self.api_key = api_key or os.getenv('CIRQ_IONQ_API_KEY') or os.getenv('IONQ_API_KEY')
if not self.api_key:
raise EnvironmentError(
'Parameter api_key was not specified and the environment variable '
Expand Down
66 changes: 61 additions & 5 deletions cirq-ionq/cirq_ionq/service_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,25 +158,81 @@ def test_service_list_calibrations():
mock_client.list_calibrations.assert_called_with(start=start, end=end, limit=10, batch_size=2)


@mock.patch.dict(os.environ, {'IONQ_API_KEY': 'tomyheart'})
def test_service_api_key_via_env():
os.environ['IONQ_API_KEY'] = 'tomyheart'
service = ionq.Service(remote_host='http://example.com')
assert service.api_key == 'tomyheart'
del os.environ['IONQ_API_KEY']


@mock.patch.dict(os.environ, {'IONQ_REMOTE_HOST': 'http://example.com'})
def test_service_remote_host_via_env():
os.environ['IONQ_REMOTE_HOST'] = 'http://example.com'
service = ionq.Service(api_key='tomyheart')
assert service.remote_host == 'http://example.com'
del os.environ['IONQ_REMOTE_HOST']


@mock.patch.dict(os.environ, {}, clear=True)
def test_service_no_param_or_env_variable():
with pytest.raises(EnvironmentError):
_ = ionq.Service(remote_host='http://example.com')


def test_service_no_url_default():
@mock.patch.dict(os.environ, {'IONQ_API_KEY': 'not_this_key'})
def test_service_api_key_passed_directly():
service = ionq.Service(remote_host='http://example.com', api_key='tomyheart')
assert service.api_key == 'tomyheart'


@mock.patch.dict(os.environ, {'CIRQ_IONQ_API_KEY': 'tomyheart'})
def test_service_api_key_from_env_var_cirq_ionq():
service = ionq.Service(remote_host='http://example.com')
assert service.api_key == 'tomyheart'


@mock.patch.dict(os.environ, {'IONQ_API_KEY': 'tomyheart'})
def test_service_api_key_from_env_var_ionq():
service = ionq.Service(remote_host='http://example.com')
assert service.api_key == 'tomyheart'


@mock.patch.dict(os.environ, {}, clear=True)
def test_service_api_key_not_found_raises_error():
with pytest.raises(EnvironmentError):
_ = ionq.Service(remote_host='http://example.com')


@mock.patch.dict(os.environ, {'CIRQ_IONQ_API_KEY': 'tomyheart', 'IONQ_API_KEY': 'not_this_key'})
def test_service_api_key_from_env_var_cirq_ionq_precedence():
service = ionq.Service(remote_host='http://example.com')
assert service.api_key == 'tomyheart'


@mock.patch.dict(os.environ, {'CIRQ_IONQ_REMOTE_HOST': 'not_this_host'})
def test_service_remote_host_passed_directly():
service = ionq.Service(remote_host='http://example.com', api_key='tomyheart')
assert service.remote_host == 'http://example.com'


@mock.patch.dict(os.environ, {'CIRQ_IONQ_REMOTE_HOST': 'http://example.com'})
def test_service_remote_host_from_env_var_cirq_ionq():
service = ionq.Service(api_key='tomyheart')
assert service.remote_host == 'http://example.com'


@mock.patch.dict(os.environ, {'IONQ_REMOTE_HOST': 'http://example.com'})
def test_service_remote_host_from_env_var_ionq():
service = ionq.Service(api_key='tomyheart')
assert service.remote_host == 'http://example.com'


@mock.patch.dict(os.environ, {}, clear=True)
def test_service_remote_host_default():
service = ionq.Service(api_key='tomyheart', api_version='v0.1')
assert service.remote_host == 'https://api.ionq.co/v0.1'


@mock.patch.dict(
os.environ, {'CIRQ_IONQ_REMOTE_HOST': 'http://example.com', 'IONQ_REMOTE_HOST': 'not_this_host'}
)
def test_service_remote_host_from_env_var_cirq_ionq_precedence():
service = ionq.Service(api_key='tomyheart')
assert service.remote_host == 'http://example.com'