@@ -42,13 +42,13 @@ def _get_job_results(output_data_format: str, results_as_json_str: str, status:
4242 return job .get_results ()
4343
4444
45- def _get_job_results_histogram (output_data_format : str , results_as_json_str : str ):
46- job = _mock_job (output_data_format , results_as_json_str )
45+ def _get_job_results_histogram (output_data_format : str , results_as_json_str : str , status : str = "Succeeded" ):
46+ job = _mock_job (output_data_format , results_as_json_str , status )
4747 return job .get_results_histogram ()
4848
4949
50- def _get_job_results_shots (output_data_format : str , results_as_json_str : str ):
51- job = _mock_job (output_data_format , results_as_json_str )
50+ def _get_job_results_shots (output_data_format : str , results_as_json_str : str , status : str = "Succeeded" ):
51+ job = _mock_job (output_data_format , results_as_json_str , status )
5252 return job .get_results_shots ()
5353
5454
@@ -99,6 +99,66 @@ def test_job_get_results_with_cancelled_status_raises_runtime_error():
9999 )
100100
101101
102+ def test_job_get_results_histogram_with_completed_status ():
103+ job_results = _get_job_results_histogram (
104+ "microsoft.quantum-results.v2" ,
105+ '{"DataFormat": "microsoft.quantum-results.v2", "Results": [{"Histogram": [{"Outcome": [0], "Display": "[0]", "Count": 2}, {"Outcome": [1], "Display": "[1]", "Count": 2}], "Shots": [[0], [1], [1], [0]]}]}' ,
106+ "Completed" ,
107+ )
108+ assert len (job_results .keys ()) == 2
109+ assert job_results ["[0]" ]["count" ] == 2
110+ assert job_results ["[1]" ]["count" ] == 2
111+
112+
113+ def test_job_get_results_histogram_with_failed_status_raises_runtime_error ():
114+ with pytest .raises (RuntimeError , match = "Cannot retrieve results as job execution failed" ):
115+ _get_job_results_histogram (
116+ "microsoft.quantum-results.v2" ,
117+ '{"DataFormat": "microsoft.quantum-results.v2", "Results": [{"Histogram": [{"Outcome": [0], "Display": "[0]", "Count": 2}, {"Outcome": [1], "Display": "[1]", "Count": 2}], "Shots": [[0], [1], [1], [0]]}]}' ,
118+ "Failed" ,
119+ )
120+
121+
122+ def test_job_get_results_histogram_with_cancelled_status_raises_runtime_error ():
123+ with pytest .raises (RuntimeError , match = "Cannot retrieve results as job execution failed" ):
124+ _get_job_results_histogram (
125+ "microsoft.quantum-results.v2" ,
126+ '{"DataFormat": "microsoft.quantum-results.v2", "Results": [{"Histogram": [{"Outcome": [0], "Display": "[0]", "Count": 2}, {"Outcome": [1], "Display": "[1]", "Count": 2}], "Shots": [[0], [1], [1], [0]]}]}' ,
127+ "Cancelled" ,
128+ )
129+
130+
131+ def test_job_get_results_shots_with_completed_status ():
132+ job_results = _get_job_results_shots (
133+ "microsoft.quantum-results.v2" ,
134+ '{"DataFormat": "microsoft.quantum-results.v2", "Results": [{"Histogram": [{"Outcome": [0], "Display": "[0]", "Count": 2}, {"Outcome": [1], "Display": "[1]", "Count": 2}], "Shots": [[0], [1], [1], [0]]}]}' ,
135+ "Completed" ,
136+ )
137+ assert len (job_results ) == 4
138+ assert job_results [0 ] == [0 ]
139+ assert job_results [1 ] == [1 ]
140+ assert job_results [2 ] == [1 ]
141+ assert job_results [3 ] == [0 ]
142+
143+
144+ def test_job_get_results_shots_with_failed_status_raises_runtime_error ():
145+ with pytest .raises (RuntimeError , match = "Cannot retrieve results as job execution failed" ):
146+ _get_job_results_shots (
147+ "microsoft.quantum-results.v2" ,
148+ '{"DataFormat": "microsoft.quantum-results.v2", "Results": [{"Histogram": [{"Outcome": [0], "Display": "[0]", "Count": 2}, {"Outcome": [1], "Display": "[1]", "Count": 2}], "Shots": [[0], [1], [1], [0]]}]}' ,
149+ "Failed" ,
150+ )
151+
152+
153+ def test_job_get_results_shots_with_cancelled_status_raises_runtime_error ():
154+ with pytest .raises (RuntimeError , match = "Cannot retrieve results as job execution failed" ):
155+ _get_job_results_shots (
156+ "microsoft.quantum-results.v2" ,
157+ '{"DataFormat": "microsoft.quantum-results.v2", "Results": [{"Histogram": [{"Outcome": [0], "Display": "[0]", "Count": 2}, {"Outcome": [1], "Display": "[1]", "Count": 2}], "Shots": [[0], [1], [1], [0]]}]}' ,
158+ "Cancelled" ,
159+ )
160+
161+
102162def test_job_for_microsoft_quantum_results_v1_no_histogram_returns_raw_result ():
103163 job_result_raw = '{"NotHistogramProperty": ["[0]", 0.50, "[1]", 0.50]}'
104164 job_result = _get_job_results ("microsoft.quantum-results.v1" , job_result_raw )
@@ -325,6 +385,143 @@ def test_job_for_microsoft_quantum_results_shots_v2_tuple_success():
325385 assert job_results [2 ] == [1 ]
326386
327387
388+ def test_job_for_microsoft_quantum_results_shots_v2_error_in_shots ():
389+ output = """
390+ {
391+ "DataFormat": "microsoft.quantum-results.v2",
392+ "Results": [
393+ {
394+ "Histogram": [
395+ {
396+ "Outcome": [10],
397+ "Display": "[10]",
398+ "Count": 3
399+ },
400+ {
401+ "Outcome": {
402+ "Error": {
403+ "Code": "0x20",
404+ "Name": "TestErrorThirtyTwo"
405+ }
406+ },
407+ "Display": "Error 0x20: TestErrorThirtyTwo",
408+ "Count": 1
409+ },
410+ {
411+ "Outcome": {
412+ "Error": {
413+ "Code": "0x40",
414+ "Name": "TestErrorSixtyFour"
415+ }
416+ },
417+ "Display": "Error 0x40: TestErrorSixtyFour",
418+ "Count": 1
419+ }
420+ ],
421+ "Shots": [
422+ [10],
423+ {
424+ "Error": {
425+ "Code": "0x20",
426+ "Name": "TestErrorThirtyTwo",
427+ "Foo": "42",
428+ "Bar": "baz"
429+ }
430+ },
431+ [10],
432+ {
433+ "Error": {
434+ "Code": "0x40",
435+ "Name": "TestErrorSixtyFour",
436+ "Arg0": "99",
437+ "Arg1": "33"
438+ }
439+ },
440+ [10]
441+ ]
442+ }
443+ ]
444+ }
445+ """
446+
447+ job_results = _get_job_results_shots ("microsoft.quantum-results.v2" , output )
448+ assert len (job_results ) == 5
449+ assert job_results [0 ] == [10 ]
450+ assert job_results [1 ] == {"Error" : {"Code" : "0x20" , "Name" : "TestErrorThirtyTwo" , "Foo" : "42" , "Bar" : "baz" }}
451+ assert job_results [2 ] == [10 ]
452+ assert job_results [3 ] == {"Error" : {"Code" : "0x40" , "Name" : "TestErrorSixtyFour" , "Arg0" : "99" , "Arg1" : "33" }}
453+ assert job_results [4 ] == [10 ]
454+
455+
456+ def test_job_for_microsoft_quantum_results_histogram_v2_error_in_histogram ():
457+ output = """
458+ {
459+ "DataFormat": "microsoft.quantum-results.v2",
460+ "Results": [
461+ {
462+ "Histogram": [
463+ {
464+ "Outcome": [10],
465+ "Display": "[10]",
466+ "Count": 3
467+ },
468+ {
469+ "Outcome": {
470+ "Error": {
471+ "Code": "0x20",
472+ "Name": "TestErrorThirtyTwo"
473+ }
474+ },
475+ "Display": "Error 0x20: TestErrorThirtyTwo",
476+ "Count": 1
477+ },
478+ {
479+ "Outcome": {
480+ "Error": {
481+ "Code": "0x40",
482+ "Name": "TestErrorSixtyFour"
483+ }
484+ },
485+ "Display": "Error 0x40: TestErrorSixtyFour",
486+ "Count": 1
487+ }
488+ ],
489+ "Shots": [
490+ [10],
491+ {
492+ "Error": {
493+ "Code": "0x20",
494+ "Name": "TestErrorThirtyTwo",
495+ "Foo": "42",
496+ "Bar": "baz"
497+ }
498+ },
499+ [10],
500+ {
501+ "Error": {
502+ "Code": "0x40",
503+ "Name": "TestErrorSixtyFour",
504+ "Arg0": "99",
505+ "Arg1": "33"
506+ }
507+ },
508+ [10]
509+ ]
510+ }
511+ ]
512+ }
513+ """
514+
515+ job_results = _get_job_results_histogram ("microsoft.quantum-results.v2" , output )
516+ assert len (job_results .keys ()) == 3
517+ assert job_results ["[10]" ]["count" ] == 3
518+ assert job_results ["Error 0x20: TestErrorThirtyTwo" ]["count" ] == 1
519+ assert job_results ["Error 0x40: TestErrorSixtyFour" ]["count" ] == 1
520+ assert job_results ["[10]" ]["outcome" ] == [10 ]
521+ assert job_results ["Error 0x20: TestErrorThirtyTwo" ]["outcome" ] == {"Error" : {"Code" : "0x20" , "Name" : "TestErrorThirtyTwo" }}
522+ assert job_results ["Error 0x40: TestErrorSixtyFour" ]["outcome" ] == {"Error" : {"Code" : "0x40" , "Name" : "TestErrorSixtyFour" }}
523+
524+
328525def test_job_for_microsoft_quantum_results_shots_v2_wrong_type_raises_exception ():
329526 try :
330527 _get_job_results_shots (
0 commit comments