3

I'm writing a Q# program and I'm calling an operation defined in the program that outputs a Result (One or Zero).

For example something like this:

output = QuantumOperation.Run(sim).Result;

I want to convert now output to a bool within C#, but I don't know how.

One way to go around this is to change the output of QuantumOperation to be a bool and transforming the result at the end of the operation with ResultAsBool(result). However, I want to maintain the output of the operation to be a Result. So ideally the conversion should happen within the C# host code.

1 Answers1

4

Q# type Result is represented by C# class Microsoft.Quantum.Simulation.Core.Result; in particular, it has constants Result.Zero and Result.One that you can compare the return of your operation to.

using Microsoft.Quantum.Simulation.Core;

boolOutput = (QuantumOperation.Run(sim).Result == Result.One);
Mariia Mykhailova
  • 9,010
  • 1
  • 12
  • 39