Repository: microsoft/qsharp
Pull request #709 (2023-09-14 20:07:31)

Revert "Fixes a bug in single output recording (#692)"
This reverts commit 192368ca666ac37f8e97dc3a73e3e8efbff1da3f.


Patch:
diff --git a/compiler/qsc_codegen/src/qir_base.rs b/compiler/qsc_codegen/src/qir_base.rs
index 7548d774..9bb7fa99 100644
--- a/compiler/qsc_codegen/src/qir_base.rs
+++ b/compiler/qsc_codegen/src/qir_base.rs
@@ -170,39 +170,25 @@ impl BaseProfSim {
     }
 
     fn write_output_recording(&mut self, val: &Value) -> std::fmt::Result {
-        fn write_output_recording_recursive(
-            this: &mut BaseProfSim,
-            val: &Value,
-        ) -> std::fmt::Result {
-            match val {
-                Value::Array(arr) => {
-                    this.write_array_recording(arr.len())?;
-                    for val in arr.iter() {
-                        write_output_recording_recursive(this, val)?;
-                    }
+        match val {
+            Value::Array(arr) => {
+                self.write_array_recording(arr.len())?;
+                for val in arr.iter() {
+                    self.write_output_recording(val)?;
                 }
-                Value::Result(r) => {
-                    this.write_result_recording(r.unwrap_id());
-                }
-                Value::Tuple(tup) => {
-                    this.write_tuple_recording(tup.len())?;
-                    for val in tup.iter() {
-                        write_output_recording_recursive(this, val)?;
-                    }
+            }
+            Value::Result(r) => {
+                self.write_result_recording(r.unwrap_id());
+            }
+            Value::Tuple(tup) => {
+                self.write_tuple_recording(tup.len())?;
+                for val in tup.iter() {
+                    self.write_output_recording(val)?;
                 }
-                _ => panic!("unexpected value type: {val:?}"),
             }
-            Ok(())
+            _ => panic!("unexpected value type: {val:?}"),
         }
-
-        if matches!(val, Value::Result(_)) {
-            // This is a temporary workaround to avoid output recording bug
-            // that fails when only a result is returned. This treats a single
-            // result as if it were a tuple of arity 1.
-            self.write_tuple_recording(1)?;
-        }
-
-        write_output_recording_recursive(self, val)
+        Ok(())
     }
 
     fn write_result_recording(&mut self, res: usize) {


Test patch:
diff --git a/compiler/qsc/src/interpret/stateful/tests.rs b/compiler/qsc/src/interpret/stateful/tests.rs
index 9d4dbb6a..c5f2f49f 100644
--- a/compiler/qsc/src/interpret/stateful/tests.rs
+++ b/compiler/qsc/src/interpret/stateful/tests.rs
@@ -567,7 +567,6 @@ mod given_interpreter {
                 define void @ENTRYPOINT__main() #0 {
                   call void @__quantum__qis__m__body(%Qubit* inttoptr (i64 0 to %Qubit*), %Result* inttoptr (i64 0 to %Result*)) #1
                   call void @__quantum__qis__reset__body(%Qubit* inttoptr (i64 0 to %Qubit*))
-                  call void @__quantum__rt__tuple_record_output(i64 1, i8* null)
                   call void @__quantum__rt__result_record_output(%Result* inttoptr (i64 0 to %Result*), i8* null)
                   ret void
                 }
@@ -634,7 +633,6 @@ mod given_interpreter {
                 define void @ENTRYPOINT__main() #0 {
                   call void @__quantum__qis__m__body(%Qubit* inttoptr (i64 0 to %Qubit*), %Result* inttoptr (i64 0 to %Result*)) #1
                   call void @__quantum__qis__reset__body(%Qubit* inttoptr (i64 0 to %Qubit*))
-                  call void @__quantum__rt__tuple_record_output(i64 1, i8* null)
                   call void @__quantum__rt__result_record_output(%Result* inttoptr (i64 0 to %Result*), i8* null)
                   ret void
                 }
@@ -703,7 +701,6 @@ mod given_interpreter {
                 define void @ENTRYPOINT__main() #0 {
                   call void @__quantum__qis__m__body(%Qubit* inttoptr (i64 0 to %Qubit*), %Result* inttoptr (i64 0 to %Result*)) #1
                   call void @__quantum__qis__reset__body(%Qubit* inttoptr (i64 0 to %Qubit*))
-                  call void @__quantum__rt__tuple_record_output(i64 1, i8* null)
                   call void @__quantum__rt__result_record_output(%Result* inttoptr (i64 0 to %Result*), i8* null)
                   ret void
                 }
@@ -811,7 +808,6 @@ mod given_interpreter {
                 define void @ENTRYPOINT__main() #0 {
                   call void @__quantum__qis__m__body(%Qubit* inttoptr (i64 0 to %Qubit*), %Result* inttoptr (i64 0 to %Result*)) #1
                   call void @__quantum__qis__reset__body(%Qubit* inttoptr (i64 0 to %Qubit*))
-                  call void @__quantum__rt__tuple_record_output(i64 1, i8* null)
                   call void @__quantum__rt__result_record_output(%Result* inttoptr (i64 0 to %Result*), i8* null)
                   ret void
                 }
@@ -893,7 +889,6 @@ mod given_interpreter {
                 define void @ENTRYPOINT__main() #0 {
                   call void @__quantum__qis__m__body(%Qubit* inttoptr (i64 0 to %Qubit*), %Result* inttoptr (i64 0 to %Result*)) #1
                   call void @__quantum__qis__reset__body(%Qubit* inttoptr (i64 0 to %Qubit*))
-                  call void @__quantum__rt__tuple_record_output(i64 1, i8* null)
                   call void @__quantum__rt__result_record_output(%Result* inttoptr (i64 0 to %Result*), i8* null)
                   ret void
                 }
diff --git a/compiler/qsc_codegen/src/qir_base/tests.rs b/compiler/qsc_codegen/src/qir_base/tests.rs
index 0914f6fe..f91be0a6 100644
--- a/compiler/qsc_codegen/src/qir_base/tests.rs
+++ b/compiler/qsc_codegen/src/qir_base/tests.rs
@@ -69,7 +69,6 @@ fn simple_entry_program_is_valid() {
             define void @ENTRYPOINT__main() #0 {
               call void @__quantum__qis__h__body(%Qubit* inttoptr (i64 0 to %Qubit*))
               call void @__quantum__qis__m__body(%Qubit* inttoptr (i64 0 to %Qubit*), %Result* inttoptr (i64 0 to %Result*)) #1
-              call void @__quantum__rt__tuple_record_output(i64 1, i8* null)
               call void @__quantum__rt__result_record_output(%Result* inttoptr (i64 0 to %Result*), i8* null)
               ret void
             }
@@ -133,7 +132,6 @@ fn simple_program_is_valid() {
             define void @ENTRYPOINT__main() #0 {
               call void @__quantum__qis__h__body(%Qubit* inttoptr (i64 0 to %Qubit*))
               call void @__quantum__qis__m__body(%Qubit* inttoptr (i64 0 to %Qubit*), %Result* inttoptr (i64 0 to %Result*)) #1
-              call void @__quantum__rt__tuple_record_output(i64 1, i8* null)
               call void @__quantum__rt__result_record_output(%Result* inttoptr (i64 0 to %Result*), i8* null)
               ret void
             }
diff --git a/pip/tests-qir/test_qir.py b/pip/tests-qir/test_qir.py
index 78adb6e6..9f276ea9 100644
--- a/pip/tests-qir/test_qir.py
+++ b/pip/tests-qir/test_qir.py
@@ -16,19 +16,19 @@ def test_compile_qir_input_data() -> None:
     assert module.functions[0].name == "ENTRYPOINT__main"
     func = module.functions[0]
     assert len(func.basic_blocks) == 1
-    assert len(func.basic_blocks[0].instructions) == 4
+    assert len(func.basic_blocks[0].instructions) == 3
     call_m = func.basic_blocks[0].instructions[0]
     assert isinstance(call_m, Call)
     assert call_m.callee.name == "__quantum__qis__m__body"
     assert len(call_m.args) == 2
     assert qubit_id(call_m.args[0]) == 0
     assert result_id(call_m.args[1]) == 0
-    record_res = func.basic_blocks[0].instructions[2]
+    record_res = func.basic_blocks[0].instructions[1]
     assert isinstance(record_res, Call)
     assert len(record_res.args) == 2
     assert record_res.callee.name == "__quantum__rt__result_record_output"
     assert result_id(record_res.args[0]) == 0
-    assert func.basic_blocks[0].instructions[3].opcode == Opcode.RET
+    assert func.basic_blocks[0].instructions[2].opcode == Opcode.RET
 
 
 def test_compile_qir_all_gates() -> None: