Repository: microsoft/qsharp
Pull request #1394 (2024-04-16 16:52:39)

Add tcNickolas to samples codeowners

github-actions[bot]: ## Benchmark for 680a6b5
  <details>
    <summary>Click to view benchmark</summary>

| Test | Base         | PR               | % |
|------|--------------|------------------|---|
| Array append evaluation | **337.4±13.68µs** | 344.6±6.20µs | **+2.13%** |
| Array literal evaluation | **170.8±0.87µs** | 189.6±0.74µs | **+11.01%** |
| Array update evaluation | **415.3±7.28µs** | 428.7±2.03µs | **+3.23%** |
| Core + Standard library compilation | **18.7±0.98ms** | 19.8±0.98ms | **+5.88%** |
| Deutsch-Jozsa evaluation | 5.1±0.05ms | 5.1±0.06ms | 0.00% |
| Large file parity evaluation | 33.8±0.15ms | 33.8±0.26ms | 0.00% |
| Large input file compilation | **12.2±0.46ms** | 12.7±0.59ms | **+4.10%** |
| Large input file compilation (interpreter) | 48.3±1.22ms | 48.5±1.21ms | +0.41% |
| Large nested iteration | **32.8±0.32ms** | 34.4±0.31ms | **+4.88%** |
| Perform Runtime Capabilities Analysis (RCA) on Deutsch-Jozsa sample | 1528.0±76.47µs | 1544.3±116.46µs | +1.07% |
| Perform Runtime Capabilities Analysis (RCA) on large file sample | **8.0±0.14ms** | 8.2±0.18ms | **+2.50%** |
| Perform Runtime Capabilities Analysis (RCA) on teleport sample | 1432.3±61.46µs | 1446.3±109.69µs | +0.98% |
| Perform Runtime Capabilities Analysis (RCA) on the core and std libraries | 21.9±0.42ms | 22.0±0.41ms | +0.46% |
| Teleport evaluation | 81.5±3.71µs | 82.1±3.59µs | +0.74% |

  </details>
  

Patch:
diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS
index b45876b8..b355249e 100644
--- a/.github/CODEOWNERS
+++ b/.github/CODEOWNERS
@@ -18,7 +18,7 @@
 /pip @billti @idavis @minestarks
 /playground @billti @cesarzc @minestarks
 /resource_estimator @billti @ivanbasov @swernli
-/samples @cesarzc @DmitryVasilevsky @sezna @swernli
+/samples @cesarzc @DmitryVasilevsky @sezna @swernli @tcNickolas
 /vscode @billti @idavis @minestarks @sezna
 /wasm @billti @cesarzc @sezna @swernli
 
diff --git a/compiler/qsc_partial_eval/src/lib.rs b/compiler/qsc_partial_eval/src/lib.rs
index f31f7f93..20021dd8 100644
--- a/compiler/qsc_partial_eval/src/lib.rs
+++ b/compiler/qsc_partial_eval/src/lib.rs
@@ -228,9 +228,22 @@ impl<'a> PartialEvaluator<'a> {
             return Err(error);
         }
 
-        // Insert the return expression and return the generated program.
+        // Insert the return expression.
         let current_block = self.get_current_block_mut();
         current_block.0.push(Instruction::Return);
+
+        // Set the number of qubits and results used by the program.
+        self.program.num_qubits = self
+            .resource_manager
+            .qubit_count()
+            .try_into()
+            .expect("qubits count should fit into a u32");
+        self.program.num_results = self
+            .resource_manager
+            .results_count()
+            .try_into()
+            .expect("results count should fit into a u32");
+
         Ok(self.program)
     }
 
diff --git a/compiler/qsc_partial_eval/src/management.rs b/compiler/qsc_partial_eval/src/management.rs
index 20fff948..09b8525f 100644
--- a/compiler/qsc_partial_eval/src/management.rs
+++ b/compiler/qsc_partial_eval/src/management.rs
@@ -17,6 +17,14 @@ pub struct ResourceManager {
 }
 
 impl ResourceManager {
+    pub fn qubit_count(&self) -> usize {
+        self.qubits_in_use.len()
+    }
+
+    pub fn results_count(&self) -> usize {
+        self.next_result
+    }
+
     pub fn allocate_qubit(&mut self) -> Qubit {
         if let Some(qubit_id) = self.qubits_in_use.iter().position(|in_use| !in_use) {
             self.qubits_in_use[qubit_id] = true;
diff --git a/playground/src/docs.tsx b/playground/src/docs.tsx
index 651e1395..bc951586 100644
--- a/playground/src/docs.tsx
+++ b/playground/src/docs.tsx
@@ -54,8 +54,8 @@ export function DocumentationDisplay(props: {
   const docsDiv = useRef<HTMLDivElement>(null);
 
   useEffect(() => {
-    if (!docsDiv.current) return;
-    docsDiv.current.innerHTML = props.documentation!.get(
+    if (!docsDiv.current || !props.documentation) return;
+    docsDiv.current.innerHTML = props.documentation.get(
       props.currentNamespace,
     )!;
     MathJax.typeset();


Test patch:
diff --git a/compiler/qsc/src/interpret/tests.rs b/compiler/qsc/src/interpret/tests.rs
index f9fd21c3..f2e76035 100644
--- a/compiler/qsc/src/interpret/tests.rs
+++ b/compiler/qsc/src/interpret/tests.rs
@@ -757,7 +757,7 @@ mod given_interpreter {
 
                 declare void @__quantum__qis__rz__body(double, %Qubit*)
 
-                attributes #0 = { "entry_point" "output_labeling_schema" "qir_profiles"="adaptive_profile" "required_num_qubits"="0" "required_num_results"="0" }
+                attributes #0 = { "entry_point" "output_labeling_schema" "qir_profiles"="adaptive_profile" "required_num_qubits"="1" "required_num_results"="0" }
                 attributes #1 = { "irreversible" }
 
                 ; module flags
diff --git a/compiler/qsc_partial_eval/tests/qubits.rs b/compiler/qsc_partial_eval/tests/qubits.rs
index a3eed5ac..db2c9e9e 100644
--- a/compiler/qsc_partial_eval/tests/qubits.rs
+++ b/compiler/qsc_partial_eval/tests/qubits.rs
@@ -50,6 +50,8 @@ fn qubit_ids_are_correct_for_allocate_use_release_one_qubit() {
             Instruction::Return,
         ],
     );
+    assert_eq!(program.num_qubits, 1);
+    assert_eq!(program.num_results, 0);
 }
 
 #[test]
@@ -97,6 +99,8 @@ fn qubit_ids_are_correct_for_allocate_use_release_multiple_qubits() {
             Instruction::Return,
         ],
     );
+    assert_eq!(program.num_qubits, 3);
+    assert_eq!(program.num_results, 0);
 }
 
 #[test]
@@ -144,6 +148,8 @@ fn qubit_ids_are_correct_for_allocate_use_release_one_qubit_multiple_times() {
             Instruction::Return,
         ],
     );
+    assert_eq!(program.num_qubits, 1);
+    assert_eq!(program.num_results, 0);
 }
 
 #[test]
@@ -207,4 +213,6 @@ fn qubit_ids_are_correct_for_allocate_use_release_multiple_qubits_interleaved()
             Instruction::Return,
         ],
     );
+    assert_eq!(program.num_qubits, 4);
+    assert_eq!(program.num_results, 0);
 }
diff --git a/compiler/qsc_partial_eval/tests/results.rs b/compiler/qsc_partial_eval/tests/results.rs
index 5676d3b0..64c3adf5 100644
--- a/compiler/qsc_partial_eval/tests/results.rs
+++ b/compiler/qsc_partial_eval/tests/results.rs
@@ -72,6 +72,8 @@ fn result_ids_are_correct_for_measuring_and_resetting_one_qubit() {
             Instruction::Return,
         ],
     );
+    assert_eq!(program.num_qubits, 1);
+    assert_eq!(program.num_results, 1);
 }
 
 #[test]
@@ -104,6 +106,8 @@ fn result_ids_are_correct_for_measuring_one_qubit() {
             Instruction::Return,
         ],
     );
+    assert_eq!(program.num_qubits, 1);
+    assert_eq!(program.num_results, 1);
 }
 
 #[test]
@@ -154,6 +158,8 @@ fn result_ids_are_correct_for_measuring_one_qubit_multiple_times() {
             Instruction::Return,
         ],
     );
+    assert_eq!(program.num_qubits, 1);
+    assert_eq!(program.num_results, 3);
 }
 
 #[test]
@@ -204,6 +210,8 @@ fn result_ids_are_correct_for_measuring_multiple_qubits() {
             Instruction::Return,
         ],
     );
+    assert_eq!(program.num_qubits, 3);
+    assert_eq!(program.num_results, 3);
 }
 
 #[test]
@@ -279,6 +287,8 @@ fn comparing_measurement_results_for_equality_adds_read_result_and_comparison_in
             Instruction::Return,
         ],
     );
+    assert_eq!(program.num_qubits, 2);
+    assert_eq!(program.num_results, 2);
 }
 
 #[test]
@@ -354,6 +364,8 @@ fn comparing_measurement_results_for_inequality_adds_read_result_and_comparison_
             Instruction::Return,
         ],
     );
+    assert_eq!(program.num_qubits, 2);
+    assert_eq!(program.num_results, 2);
 }
 
 #[test]
@@ -410,6 +422,8 @@ fn comparing_measurement_result_against_result_literal_for_equality_adds_read_re
             Instruction::Return,
         ],
     );
+    assert_eq!(program.num_qubits, 1);
+    assert_eq!(program.num_results, 1);
 }
 
 #[test]
@@ -466,4 +480,6 @@ fn comparing_measurement_result_against_result_literal_for_inequality_adds_read_
             Instruction::Return,
         ],
     );
+    assert_eq!(program.num_qubits, 1);
+    assert_eq!(program.num_results, 1);
 }
diff --git a/pip/tests/test_interpreter.py b/pip/tests/test_interpreter.py
index e770450d..b08b6cc3 100644
--- a/pip/tests/test_interpreter.py
+++ b/pip/tests/test_interpreter.py
@@ -350,7 +350,7 @@ def test_adaptive_qir_can_be_generated() -> None:
 
         declare void @__quantum__qis__rz__body(double, %Qubit*)
 
-        attributes #0 = { "entry_point" "output_labeling_schema" "qir_profiles"="adaptive_profile" "required_num_qubits"="0" "required_num_results"="0" }
+        attributes #0 = { "entry_point" "output_labeling_schema" "qir_profiles"="adaptive_profile" "required_num_qubits"="1" "required_num_results"="0" }
         attributes #1 = { "irreversible" }
 
         ; module flags