@@ -34,7 +34,7 @@ def test_coerce_observable_str(self, num_qubits):
34
34
self .assertEqual (obs , {label : 1 })
35
35
36
36
def test_coerce_observable_custom_basis (self ):
37
- """Test coerce_observable for custom allowed basis"""
37
+ """Test coerce_observable for custom al flowed basis"""
38
38
39
39
class PauliArray (ObservablesArray ):
40
40
"""Custom array allowing only Paulis, not projectors"""
@@ -127,6 +127,20 @@ def test_coerce_observable_pauli_mapping(self):
127
127
target = {key .to_label (): val for key , val in mapping .items ()}
128
128
self .assertEqual (obs , target )
129
129
130
+ def test_coerce_0d (self ):
131
+ """Test the coerce() method with 0-d input."""
132
+ obs = ObservablesArray .coerce ("X" )
133
+ self .assertEqual (obs .shape , ())
134
+ self .assertDictAlmostEqual (obs [()], {"X" : 1 })
135
+
136
+ obs = ObservablesArray .coerce ({"I" : 2 })
137
+ self .assertEqual (obs .shape , ())
138
+ self .assertDictAlmostEqual (obs [()], {"I" : 2 })
139
+
140
+ obs = ObservablesArray .coerce (qi .SparsePauliOp (["X" , "Y" ], [1 , 3 ]))
141
+ self .assertEqual (obs .shape , ())
142
+ self .assertDictAlmostEqual (obs [()], {"X" : 1 , "Y" : 3 })
143
+
130
144
def test_format_invalid_mapping_qubits (self ):
131
145
"""Test an error is raised when different qubits in mapping keys"""
132
146
mapping = {"IX" : 1 , "XXX" : 2 }
0 commit comments