@@ -55,53 +55,54 @@ def flatten(val: Any) -> Tuple[Any, 'ExpressionMap']:
55
55
are described above.
56
56
57
57
Examples:
58
- >>> qubit = cirq.LineQubit(0)
59
- >>> a = sympy.Symbol('a')
60
- >>> circuit = cirq.Circuit(
61
- ... cirq.X(qubit) ** (a/4),
62
- ... cirq.Y(qubit) ** (1-a/2),
63
- ... )
64
- >>> print(circuit)
65
- 0: ───X^(a/4)───Y^(1 - a/2)───
66
-
67
- >>> sweep = cirq.Linspace(a, start=0, stop=3, length=4)
68
- >>> print(cirq.ListSweep(sweep))
69
- Sweep:
70
- {'a': 0.0}
71
- {'a': 1.0}
72
- {'a': 2.0}
73
- {'a': 3.0}
74
-
75
- >>> c_flat, expr_map = cirq.flatten(circuit)
76
- >>> print(c_flat)
77
- 0: ───X^(<a/4>)───Y^(<1 - a/2>)───
78
- >>> expr_map
79
- cirq.ExpressionMap({a/4: <a/4>, 1 - a/2: <1 - a/2>})
80
-
81
- >>> new_sweep = expr_map.transform_sweep(sweep)
82
- >>> print(new_sweep)
83
- Sweep:
84
- {'<a/4>': 0.0, '<1 - a/2>': 1.0}
85
- {'<a/4>': 0.25, '<1 - a/2>': 0.5}
86
- {'<a/4>': 0.5, '<1 - a/2>': 0.0}
87
- {'<a/4>': 0.75, '<1 - a/2>': -0.5}
88
-
89
- >>> for params in sweep: # Original
90
- ... print(circuit,
91
- ... '=>',
92
- ... cirq.resolve_parameters(circuit, params))
93
- 0: ───X^(a/4)───Y^(1 - a/2)─── => 0: ───X^0───Y───
94
- 0: ───X^(a/4)───Y^(1 - a/2)─── => 0: ───X^0.25───Y^0.5───
95
- 0: ───X^(a/4)───Y^(1 - a/2)─── => 0: ───X^0.5───Y^0───
96
- 0: ───X^(a/4)───Y^(1 - a/2)─── => 0: ───X^0.75───Y^-0.5───
97
-
98
- >>> for params in new_sweep: # Flattened
99
- ... print(c_flat, '=>', end=' ')
100
- ... print(cirq.resolve_parameters(c_flat, params))
101
- 0: ───X^(<a/4>)───Y^(<1 - a/2>)─── => 0: ───X^0───Y───
102
- 0: ───X^(<a/4>)───Y^(<1 - a/2>)─── => 0: ───X^0.25───Y^0.5───
103
- 0: ───X^(<a/4>)───Y^(<1 - a/2>)─── => 0: ───X^0.5───Y^0───
104
- 0: ───X^(<a/4>)───Y^(<1 - a/2>)─── => 0: ───X^0.75───Y^-0.5───
58
+
59
+ >>> qubit = cirq.LineQubit(0)
60
+ >>> a = sympy.Symbol('a')
61
+ >>> circuit = cirq.Circuit(
62
+ ... cirq.X(qubit) ** (a/4),
63
+ ... cirq.Y(qubit) ** (1-a/2),
64
+ ... )
65
+ >>> print(circuit)
66
+ 0: ───X^(a/4)───Y^(1 - a/2)───
67
+
68
+ >>> sweep = cirq.Linspace(a, start=0, stop=3, length=4)
69
+ >>> print(cirq.ListSweep(sweep))
70
+ Sweep:
71
+ {'a': 0.0}
72
+ {'a': 1.0}
73
+ {'a': 2.0}
74
+ {'a': 3.0}
75
+
76
+ >>> c_flat, expr_map = cirq.flatten(circuit)
77
+ >>> print(c_flat)
78
+ 0: ───X^(<a/4>)───Y^(<1 - a/2>)───
79
+ >>> expr_map
80
+ cirq.ExpressionMap({a/4: <a/4>, 1 - a/2: <1 - a/2>})
81
+
82
+ >>> new_sweep = expr_map.transform_sweep(sweep)
83
+ >>> print(new_sweep)
84
+ Sweep:
85
+ {'<a/4>': 0.0, '<1 - a/2>': 1.0}
86
+ {'<a/4>': 0.25, '<1 - a/2>': 0.5}
87
+ {'<a/4>': 0.5, '<1 - a/2>': 0.0}
88
+ {'<a/4>': 0.75, '<1 - a/2>': -0.5}
89
+
90
+ >>> for params in sweep: # Original
91
+ ... print(circuit,
92
+ ... '=>',
93
+ ... cirq.resolve_parameters(circuit, params))
94
+ 0: ───X^(a/4)───Y^(1 - a/2)─── => 0: ───X^0───Y───
95
+ 0: ───X^(a/4)───Y^(1 - a/2)─── => 0: ───X^0.25───Y^0.5───
96
+ 0: ───X^(a/4)───Y^(1 - a/2)─── => 0: ───X^0.5───Y^0───
97
+ 0: ───X^(a/4)───Y^(1 - a/2)─── => 0: ───X^0.75───Y^-0.5───
98
+
99
+ >>> for params in new_sweep: # Flattened
100
+ ... print(c_flat, '=>', end=' ')
101
+ ... print(cirq.resolve_parameters(c_flat, params))
102
+ 0: ───X^(<a/4>)───Y^(<1 - a/2>)─── => 0: ───X^0───Y───
103
+ 0: ───X^(<a/4>)───Y^(<1 - a/2>)─── => 0: ───X^0.25───Y^0.5───
104
+ 0: ───X^(<a/4>)───Y^(<1 - a/2>)─── => 0: ───X^0.5───Y^0───
105
+ 0: ───X^(<a/4>)───Y^(<1 - a/2>)─── => 0: ───X^0.75───Y^-0.5───
105
106
"""
106
107
flattener = _ParamFlattener ()
107
108
val_flat = flattener .flatten (val )
0 commit comments