Skip to content

Convert cirq.Result to ABC and add ResultDict implementation #4806

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

Merged
merged 7 commits into from
Jan 26, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cirq-aqt/cirq_aqt/aqt_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def run_sweep(
)
results = results.astype(bool)
res_dict = {meas_name: results}
trial_results.append(cirq.Result(params=param_resolver, measurements=res_dict))
trial_results.append(cirq.ResultDict(params=param_resolver, measurements=res_dict))
return trial_results


Expand Down
1 change: 1 addition & 0 deletions cirq-core/cirq/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,7 @@
flatten,
flatten_with_params,
flatten_with_sweep,
ResultDict,
Linspace,
ListSweep,
ParamDictType,
Expand Down
4 changes: 2 additions & 2 deletions cirq-core/cirq/contrib/quantum_volume/quantum_volume_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def test_sample_heavy_set():

sampler = Mock(spec=cirq.Simulator)
# Construct a result that returns "1", "2", "3", "0"
result = cirq.Result(
result = cirq.ResultDict(
params=cirq.ParamResolver({}),
measurements={'mock': np.array([[0, 1], [1, 0], [1, 1], [0, 0]])},
)
Expand All @@ -94,7 +94,7 @@ def test_sample_heavy_set_with_parity():
# bitstring "10" is valid and heavy. The second "01" is valid and not
# heavy. The third and fourth bitstrings "11" and "00" are not valid and
# dropped.
result = cirq.Result(
result = cirq.ResultDict(
params=cirq.ParamResolver({}),
measurements={
'0': np.array([[1], [0]]),
Expand Down
5 changes: 3 additions & 2 deletions cirq-core/cirq/json_resolver_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ def _parallel_gate_op(gate, qubits):
'TensoredConfusionMatrices': cirq.TensoredConfusionMatrices,
'RepetitionsStoppingCriteria': cirq.work.RepetitionsStoppingCriteria,
'ResetChannel': cirq.ResetChannel,
'Result': cirq.Result,
'Result': cirq.ResultDict, # Keep support for Cirq < 0.14.
'ResultDict': cirq.ResultDict,
'Rx': cirq.Rx,
'Ry': cirq.Ry,
'Rz': cirq.Rz,
Expand All @@ -158,7 +159,7 @@ def _parallel_gate_op(gate, qubits):
'SympyCondition': cirq.SympyCondition,
'TaggedOperation': cirq.TaggedOperation,
'TiltedSquareLattice': cirq.TiltedSquareLattice,
'TrialResult': cirq.Result, # keep support for Cirq < 0.11.
'TrialResult': cirq.ResultDict, # keep support for Cirq < 0.11.
'TwoQubitGateTabulation': cirq.TwoQubitGateTabulation,
'_UnconstrainedDevice': cirq.devices.unconstrained_device._UnconstrainedDevice,
'VarianceStoppingCriteria': cirq.work.VarianceStoppingCriteria,
Expand Down
1 change: 0 additions & 1 deletion cirq-core/cirq/protocols/json_test_data/Result.repr

This file was deleted.

1 change: 1 addition & 0 deletions cirq-core/cirq/protocols/json_test_data/Result.repr_inward
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[cirq.ResultDict(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), measurements={'m': np.array([[True, True, False, True, False], [False, True, True, False, False], [True, False, True, False, True]], dtype=bool)}), cirq.ResultDict(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), measurements={'m': np.array([[1, 1, 0, 1, 0], [0, 1, 1, 0, 0], [1, 0, 1, 0, 1]], dtype=np.uint8)}), cirq.ResultDict(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), measurements={'m': np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]], dtype=np.int8)}), cirq.ResultDict(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), measurements={'m': np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]], dtype=np.int16)}), cirq.ResultDict(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), measurements={'m': np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]], dtype=np.int32)}), cirq.ResultDict(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), measurements={'m': np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]], dtype=np.int64)}), cirq.ResultDict(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), measurements={'m': np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]], dtype=np.uint8)}), cirq.ResultDict(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), measurements={'m': np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]], dtype=np.uint16)}), cirq.ResultDict(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), measurements={'m': np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]], dtype=np.uint32)}), cirq.ResultDict(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), measurements={'m': np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]], dtype=np.uint64)}), cirq.ResultDict(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), measurements={'m': np.array([[1, 1, 0, 1, 0], [0, 1, 1, 0, 0], [1, 0, 1, 0, 1]], dtype=np.uint8), 'n': np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]], dtype=np.int64)})]
297 changes: 297 additions & 0 deletions cirq-core/cirq/protocols/json_test_data/ResultDict.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,297 @@
[
{
"cirq_type": "ResultDict",
"params": {
"cirq_type": "ParamResolver",
"param_dict": [
[
{
"cirq_type": "sympy.Symbol",
"name": "a"
},
0.5
]
]
},
"measurements": {
"m": {
"packed_digits": "d32a",
"binary": true,
"dtype": "bool",
"shape": [
3,
5
]
}
}
},
{
"cirq_type": "ResultDict",
"params": {
"cirq_type": "ParamResolver",
"param_dict": [
[
{
"cirq_type": "sympy.Symbol",
"name": "a"
},
0.5
]
]
},
"measurements": {
"m": {
"packed_digits": "d32a",
"binary": true,
"dtype": "uint8",
"shape": [
3,
5
]
}
}
},
{
"cirq_type": "ResultDict",
"params": {
"cirq_type": "ParamResolver",
"param_dict": [
[
{
"cirq_type": "sympy.Symbol",
"name": "a"
},
0.5
]
]
},
"measurements": {
"m": {
"packed_digits": "934e554d5059010076007b276465736372273a20277c6931272c2027666f727472616e5f6f72646572273a2046616c73652c20277368617065273a2028332c2033292c207d202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020200a000102030405060708",
"binary": false,
"dtype": "int8",
"shape": [
3,
3
]
}
}
},
{
"cirq_type": "ResultDict",
"params": {
"cirq_type": "ParamResolver",
"param_dict": [
[
{
"cirq_type": "sympy.Symbol",
"name": "a"
},
0.5
]
]
},
"measurements": {
"m": {
"packed_digits": "934e554d5059010076007b276465736372273a20273c6932272c2027666f727472616e5f6f72646572273a2046616c73652c20277368617065273a2028332c2033292c207d202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020200a000001000200030004000500060007000800",
"binary": false,
"dtype": "int16",
"shape": [
3,
3
]
}
}
},
{
"cirq_type": "ResultDict",
"params": {
"cirq_type": "ParamResolver",
"param_dict": [
[
{
"cirq_type": "sympy.Symbol",
"name": "a"
},
0.5
]
]
},
"measurements": {
"m": {
"packed_digits": "934e554d5059010076007b276465736372273a20273c6934272c2027666f727472616e5f6f72646572273a2046616c73652c20277368617065273a2028332c2033292c207d202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020200a000000000100000002000000030000000400000005000000060000000700000008000000",
"binary": false,
"dtype": "int32",
"shape": [
3,
3
]
}
}
},
{
"cirq_type": "ResultDict",
"params": {
"cirq_type": "ParamResolver",
"param_dict": [
[
{
"cirq_type": "sympy.Symbol",
"name": "a"
},
0.5
]
]
},
"measurements": {
"m": {
"packed_digits": "934e554d5059010076007b276465736372273a20273c6938272c2027666f727472616e5f6f72646572273a2046616c73652c20277368617065273a2028332c2033292c207d202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020200a000000000000000001000000000000000200000000000000030000000000000004000000000000000500000000000000060000000000000007000000000000000800000000000000",
"binary": false,
"dtype": "int64",
"shape": [
3,
3
]
}
}
},
{
"cirq_type": "ResultDict",
"params": {
"cirq_type": "ParamResolver",
"param_dict": [
[
{
"cirq_type": "sympy.Symbol",
"name": "a"
},
0.5
]
]
},
"measurements": {
"m": {
"packed_digits": "934e554d5059010076007b276465736372273a20277c7531272c2027666f727472616e5f6f72646572273a2046616c73652c20277368617065273a2028332c2033292c207d202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020200a000102030405060708",
"binary": false,
"dtype": "uint8",
"shape": [
3,
3
]
}
}
},
{
"cirq_type": "ResultDict",
"params": {
"cirq_type": "ParamResolver",
"param_dict": [
[
{
"cirq_type": "sympy.Symbol",
"name": "a"
},
0.5
]
]
},
"measurements": {
"m": {
"packed_digits": "934e554d5059010076007b276465736372273a20273c7532272c2027666f727472616e5f6f72646572273a2046616c73652c20277368617065273a2028332c2033292c207d202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020200a000001000200030004000500060007000800",
"binary": false,
"dtype": "uint16",
"shape": [
3,
3
]
}
}
},
{
"cirq_type": "ResultDict",
"params": {
"cirq_type": "ParamResolver",
"param_dict": [
[
{
"cirq_type": "sympy.Symbol",
"name": "a"
},
0.5
]
]
},
"measurements": {
"m": {
"packed_digits": "934e554d5059010076007b276465736372273a20273c7534272c2027666f727472616e5f6f72646572273a2046616c73652c20277368617065273a2028332c2033292c207d202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020200a000000000100000002000000030000000400000005000000060000000700000008000000",
"binary": false,
"dtype": "uint32",
"shape": [
3,
3
]
}
}
},
{
"cirq_type": "ResultDict",
"params": {
"cirq_type": "ParamResolver",
"param_dict": [
[
{
"cirq_type": "sympy.Symbol",
"name": "a"
},
0.5
]
]
},
"measurements": {
"m": {
"packed_digits": "934e554d5059010076007b276465736372273a20273c7538272c2027666f727472616e5f6f72646572273a2046616c73652c20277368617065273a2028332c2033292c207d202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020200a000000000000000001000000000000000200000000000000030000000000000004000000000000000500000000000000060000000000000007000000000000000800000000000000",
"binary": false,
"dtype": "uint64",
"shape": [
3,
3
]
}
}
},
{
"cirq_type": "ResultDict",
"params": {
"cirq_type": "ParamResolver",
"param_dict": [
[
{
"cirq_type": "sympy.Symbol",
"name": "a"
},
0.5
]
]
},
"measurements": {
"m": {
"packed_digits": "d32a",
"binary": true,
"dtype": "uint8",
"shape": [
3,
5
]
},
"n": {
"packed_digits": "934e554d5059010076007b276465736372273a20273c6938272c2027666f727472616e5f6f72646572273a2046616c73652c20277368617065273a2028332c2033292c207d202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020200a000000000000000001000000000000000200000000000000030000000000000004000000000000000500000000000000060000000000000007000000000000000800000000000000",
"binary": false,
"dtype": "int64",
"shape": [
3,
3
]
}
}
}
]
1 change: 1 addition & 0 deletions cirq-core/cirq/protocols/json_test_data/ResultDict.repr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[cirq.ResultDict(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), measurements={'m': np.array([[True, True, False, True, False], [False, True, True, False, False], [True, False, True, False, True]], dtype=bool)}), cirq.ResultDict(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), measurements={'m': np.array([[1, 1, 0, 1, 0], [0, 1, 1, 0, 0], [1, 0, 1, 0, 1]], dtype=np.uint8)}), cirq.ResultDict(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), measurements={'m': np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]], dtype=np.int8)}), cirq.ResultDict(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), measurements={'m': np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]], dtype=np.int16)}), cirq.ResultDict(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), measurements={'m': np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]], dtype=np.int32)}), cirq.ResultDict(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), measurements={'m': np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]], dtype=np.int64)}), cirq.ResultDict(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), measurements={'m': np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]], dtype=np.uint8)}), cirq.ResultDict(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), measurements={'m': np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]], dtype=np.uint16)}), cirq.ResultDict(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), measurements={'m': np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]], dtype=np.uint32)}), cirq.ResultDict(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), measurements={'m': np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]], dtype=np.uint64)}), cirq.ResultDict(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), measurements={'m': np.array([[1, 1, 0, 1, 0], [0, 1, 1, 0, 0], [1, 0, 1, 0, 1]], dtype=np.uint8), 'n': np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]], dtype=np.int64)})]
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[cirq.Result(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), measurements={'m': np.array([[True, True, False, True, False], [False, True, True, False, False], [True, False, True, False, True]], dtype=np.bool)}), cirq.Result(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), measurements={'m': np.array([[1, 1, 0, 1, 0], [0, 1, 1, 0, 0], [1, 0, 1, 0, 1]], dtype=np.uint8)}), cirq.Result(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), measurements={'m': np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]], dtype=np.int8)}), cirq.Result(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), measurements={'m': np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]], dtype=np.int16)}), cirq.Result(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), measurements={'m': np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]], dtype=np.int32)}), cirq.Result(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), measurements={'m': np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]], dtype=np.int64)}), cirq.Result(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), measurements={'m': np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]], dtype=np.uint8)}), cirq.Result(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), measurements={'m': np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]], dtype=np.uint16)}), cirq.Result(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), measurements={'m': np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]], dtype=np.uint32)}), cirq.Result(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), measurements={'m': np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]], dtype=np.uint64)}), cirq.Result(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), measurements={'m': np.array([[1, 1, 0, 1, 0], [0, 1, 1, 0, 0], [1, 0, 1, 0, 1]], dtype=np.uint8), 'n': np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]], dtype=np.int64)})]
[cirq.ResultDict(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), measurements={'m': np.array([[True, True, False, True, False], [False, True, True, False, False], [True, False, True, False, True]], dtype=np.bool)}), cirq.ResultDict(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), measurements={'m': np.array([[1, 1, 0, 1, 0], [0, 1, 1, 0, 0], [1, 0, 1, 0, 1]], dtype=np.uint8)}), cirq.ResultDict(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), measurements={'m': np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]], dtype=np.int8)}), cirq.ResultDict(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), measurements={'m': np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]], dtype=np.int16)}), cirq.ResultDict(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), measurements={'m': np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]], dtype=np.int32)}), cirq.ResultDict(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), measurements={'m': np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]], dtype=np.int64)}), cirq.ResultDict(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), measurements={'m': np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]], dtype=np.uint8)}), cirq.ResultDict(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), measurements={'m': np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]], dtype=np.uint16)}), cirq.ResultDict(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), measurements={'m': np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]], dtype=np.uint32)}), cirq.ResultDict(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), measurements={'m': np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]], dtype=np.uint64)}), cirq.ResultDict(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), measurements={'m': np.array([[1, 1, 0, 1, 0], [0, 1, 1, 0, 0], [1, 0, 1, 0, 1]], dtype=np.uint8), 'n': np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]], dtype=np.int64)})]
2 changes: 1 addition & 1 deletion cirq-core/cirq/sim/clifford/stabilizer_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def run_sweep(
resolved_circuit,
repetitions=repetitions,
)
results.append(cirq.Result(params=param_resolver, measurements=measurements))
results.append(cirq.ResultDict(params=param_resolver, measurements=measurements))
return results

def _run(self, circuit: 'cirq.AbstractCircuit', repetitions: int) -> Dict[str, np.ndarray]:
Expand Down
Loading