@@ -82,32 +82,14 @@ def get_qcs_objects_for_notebook(
82
82
ValueError: if processor_id is not specified and no processors are available.
83
83
"""
84
84
85
- # Check for Google Application Default Credentials and run
86
- # interactive login if the notebook is executed in Colab. In
87
- # case the notebook is executed in Jupyter notebook or other
88
- # IPython runtimes, no interactive login is provided, it is
89
- # assumed that the `GOOGLE_APPLICATION_CREDENTIALS` env var is
90
- # set or `gcloud auth application-default login` was executed
91
- # already. For more information on using Application Default Credentials
92
- # see https://cloud.google.com/docs/authentication/production
93
- # Attempt to connect to the Quantum Engine API, and use a simulator if unable to connect.
94
85
if not virtual :
95
86
# Set up auth
96
87
try :
97
- from google .colab import auth
98
- except ImportError :
99
- print ("Not running in a colab kernel. Will use Application Default Credentials." )
100
- else :
101
- print ("Getting OAuth2 credentials." )
102
- print ("Press enter after entering the verification code." )
103
- try :
104
- a = auth .authenticate_user (clear_output = False )
105
- print (a )
106
- print ("Authentication complete." )
107
- except Exception as exc :
108
- print (f"Authentication failed: { exc } " )
109
- print ("Using virtual engine instead." )
110
- virtual = True
88
+ authenticate_user ()
89
+ except Exception as exc :
90
+ print (f"Authentication failed: { exc } " )
91
+ print ("Using virtual engine instead." )
92
+ virtual = True
111
93
112
94
if not virtual :
113
95
# Set up production engine
@@ -152,3 +134,46 @@ def get_qcs_objects_for_notebook(
152
134
processor_id = processor_id ,
153
135
is_simulator = is_simulator ,
154
136
)
137
+
138
+
139
+ def authenticate_user (clear_output : bool = False ) -> None :
140
+ """Authenticates on Google Cloud.
141
+
142
+ Args:
143
+ clear_output: Optional bool for whether to clear output before
144
+ authenticating. Defaults to false.
145
+
146
+ Returns:
147
+ None.
148
+
149
+ Raises:
150
+ Exception: if authentication fails.
151
+ """
152
+
153
+ # Check for Google Application Default Credentials and run
154
+ # interactive login if the notebook is executed in Colab. In
155
+ # case the notebook is executed in Jupyter notebook or other
156
+ # IPython runtimes, no interactive login is provided, it is
157
+ # assumed that the `GOOGLE_APPLICATION_CREDENTIALS` env var is
158
+ # set or `gcloud auth application-default login` was executed
159
+ # already. For more information on using Application Default Credentials
160
+ # see https://cloud.google.com/docs/authentication/production
161
+ # Attempt to connect to the Quantum Engine API, and use a simulator if unable to connect.
162
+ try :
163
+ from google .colab import auth
164
+ except ImportError :
165
+ print ("Not running in a colab kernel. Will use Application Default Credentials." )
166
+ return
167
+
168
+ try :
169
+ print ("Getting OAuth2 credentials." )
170
+ print ("Press enter after entering the verification code." )
171
+ a = auth .authenticate_user (clear_output = clear_output )
172
+ print (a )
173
+ print ("Authentication complete." )
174
+ except Exception as exc :
175
+ print (
176
+ "Authentication failed, you may not have permission to access a"
177
+ + " hardware Engine. Use a virtual Engine instead."
178
+ )
179
+ raise exc
0 commit comments