diff --git a/pip/qsharp/_qsharp.py b/pip/qsharp/_qsharp.py index aa9cfaae31..1df3d81dfe 100644 --- a/pip/qsharp/_qsharp.py +++ b/pip/qsharp/_qsharp.py @@ -426,6 +426,12 @@ def check_eq( return False return True + def as_dense_state(self) -> List[complex]: + """ + Returns the state dump as a dense list of complex amplitudes. This will include zero amplitudes. + """ + return [self.__inner.get(i, complex(0)) for i in range(2**self.qubit_count)] + def dump_machine() -> StateDump: """ diff --git a/pip/tests/test_qsharp.py b/pip/tests/test_qsharp.py index 5a8ce6e3c6..f4d57c3ec0 100644 --- a/pip/tests/test_qsharp.py +++ b/pip/tests/test_qsharp.py @@ -86,11 +86,13 @@ def test_dump_machine() -> None: assert state_dump.qubit_count == 2 assert len(state_dump) == 1 assert state_dump[2] == complex(1.0, 0.0) + assert state_dump.as_dense_state() == [0, 0, 1, 0] qsharp.eval("X(q2);") state_dump = qsharp.dump_machine() assert state_dump.qubit_count == 2 assert len(state_dump) == 1 assert state_dump[3] == complex(1.0, 0.0) + assert state_dump.as_dense_state() == [0, 0, 0, 1] qsharp.eval("H(q1);") state_dump = qsharp.dump_machine() assert state_dump.qubit_count == 2 @@ -102,6 +104,12 @@ def test_dump_machine() -> None: # in of different, potentially unnormalized states. The state should be # |01⟩: 0.7071+0.0000𝑖, |11⟩: −0.7071+0.0000𝑖 assert state_dump.check_eq({1: complex(0.7071, 0.0), 3: complex(-0.7071, 0.0)}) + assert state_dump.as_dense_state() == [ + 0, + 0.7071067811865476, + 0, + -0.7071067811865476, + ] assert state_dump.check_eq({1: complex(0.0, 0.7071), 3: complex(0.0, -0.7071)}) assert state_dump.check_eq({1: complex(0.5, 0.0), 3: complex(-0.5, 0.0)}) assert state_dump.check_eq(