Repository: microsoft/qsharp
Pull request #1679 (2024-06-28 19:55:45)

Use BoolArrayAsKetState to print help messages in CheckOracleImplementsFunction #1639
Link to the issue: https://github.com/microsoft/qsharp/issues/1639

Changes:
1. Function BoolArrayAsKetState is moved to KatasLibrary.qs
2. In addition, helper function IntArrayAsStateName is moved too.
   Reason: IntArrayAsStateName is using BoolArrayAsKetState and it is only dependency to KatasLibrary in .../distinguishing_states/Common.qs. Otherwise, dependency "Open Microsoft.Quantum.KatasLibrary;" is required
3. Message output is changed to BoolArrayAsKetState

Testing done:
1. Build "python ./build.py" in Windows cmd is successful
2. Screen output is changed (marking_oracles) - see jpeg attachments
3. No changes visible in distinguising_states
![AfterFix1639](https://github.com/microsoft/qsharp/assets/51379812/2e3bfcc7-94a2-4903-abf2-e3fbb075c0c7)
![BeforeFix1639](https://github.com/microsoft/qsharp/assets/51379812/54931287-4680-4ba0-ae24-82c35a427732)
![DistinguishingQStatesAfterFix1639](https://github.com/microsoft/qsharp/assets/51379812/51b84528-8a8f-4fe0-98f3-e47e999fdef9)




Patch:
diff --git a/compiler/qsc_circuit/src/builder.rs b/compiler/qsc_circuit/src/builder.rs
index b34b2796..c40d8d9b 100644
--- a/compiler/qsc_circuit/src/builder.rs
+++ b/compiler/qsc_circuit/src/builder.rs
@@ -26,7 +26,7 @@ impl Backend for Builder {
         let ctl0 = self.map(ctl0);
         let ctl1 = self.map(ctl1);
         let q = self.map(q);
-        self.push_gate(controlled_gate("CX", [ctl0, ctl1], [q]));
+        self.push_gate(controlled_gate("X", [ctl0, ctl1], [q]));
     }
 
     fn cx(&mut self, ctl: usize, q: usize) {
diff --git a/katas/content/KatasLibrary.qs b/katas/content/KatasLibrary.qs
index 2a489518..1f3a86a3 100644
--- a/katas/content/KatasLibrary.qs
+++ b/katas/content/KatasLibrary.qs
@@ -288,6 +288,31 @@ namespace Microsoft.Quantum.Katas {
         totalMisclassifications == 0
     }
 
+    // Helper function to convert a boolean array to its ket state representation
+    function BoolArrayAsKetState (bits : Bool[]) : String {
+        mutable stateName = "|";
+        for i in 0 .. Length(bits) - 1 {
+            set stateName += (bits[i] ? "1" | "0");
+        }
+
+        return stateName + "⟩";
+    }
+
+    // Helper function to convert an array of bit strings to its ket state representation
+    function IntArrayAsStateName (
+        qubits : Int,
+        bitStrings : Bool[][]
+    ) : String {
+        mutable statename = "";
+        for i in 0 .. Length(bitStrings) - 1 {
+            if i > 0 {
+                set statename += " + ";
+            }
+            set statename += BoolArrayAsKetState(bitStrings[i]);
+        }
+
+        return statename;
+    }
 
     /// # Summary
     /// Given a marking oracle acting on N inputs, and a classical function acting on N bits, 
@@ -319,7 +344,7 @@ namespace Microsoft.Quantum.Katas {
             ApplyPauliFromBitString(PauliX, true, binaryLE, input);
 
             if not CheckAllZero(input + [target]) {
-                Message($"Unexpected result on input {binaryLE}.");
+                Message($"Unexpected result on input {BoolArrayAsKetState(binaryLE)}.");
                 if not CheckAllZero(input) {
                     Message("The state of the input qubits changed, or they ended up entangled with the target qubit.");
                     Message("The state of the system after oracle application:");
diff --git a/katas/content/distinguishing_states/Common.qs b/katas/content/distinguishing_states/Common.qs
index 248f9167..287e9c5e 100644
--- a/katas/content/distinguishing_states/Common.qs
+++ b/katas/content/distinguishing_states/Common.qs
@@ -41,32 +41,6 @@ namespace Kata.Verification{
         }
     }
 
-    // Helper function to convert a boolean array to its ket state representation
-    function BoolArrayAsKetState (bits : Bool[]) : String {
-        mutable stateName = "|";
-        for i in 0 .. Length(bits) - 1 {
-            set stateName += (bits[i] ? "1" | "0");
-        }
-
-        return stateName + "⟩";
-    }
-
-    // Helper function to convert an array of bit strings to its ket state representation
-    function IntArrayAsStateName (
-        qubits : Int,
-        bitStrings : Bool[][]
-    ) : String {
-        mutable statename = "";
-        for i in 0 .. Length(bitStrings) - 1 {
-            if i > 0 {
-                set statename += " + ";
-            }
-            set statename += BoolArrayAsKetState(bitStrings[i]);
-        }
-
-        return statename;
-    }
-
     function StatePrep_FindFirstDiff (
         bits1 : Bool[],
         bits2 : Bool[]


Test patch:
diff --git a/compiler/qsc/src/interpret/circuit_tests.rs b/compiler/qsc/src/interpret/circuit_tests.rs
index 4a17be1d..f5d380d3 100644
--- a/compiler/qsc/src/interpret/circuit_tests.rs
+++ b/compiler/qsc/src/interpret/circuit_tests.rs
@@ -70,6 +70,33 @@ fn one_gate() {
     .assert_eq(&circ.to_string());
 }
 
+#[test]
+fn toffoli() {
+    let mut interpreter = interpreter(
+        r"
+            namespace Test {
+                @EntryPoint()
+                operation Main() : Unit {
+                    use q = Qubit[3];
+                    CCNOT(q[0], q[1], q[2]);
+                }
+            }
+        ",
+        Profile::Unrestricted,
+    );
+
+    let circ = interpreter
+        .circuit(CircuitEntryPoint::EntryPoint, false)
+        .expect("circuit generation should succeed");
+
+    expect![[r"
+        q_0    ── ● ──
+        q_1    ── ● ──
+        q_2    ── X ──
+    "]]
+    .assert_eq(&circ.to_string());
+}
+
 #[test]
 fn rotation_gate() {
     let mut interpreter = interpreter(