When coding with Q#, with the following code snippet I get an aggregate exception error (one or more errors seems to have occurred).

What is wrong with this specific code snippet, and what parts of the Q# documentation have a bearing on this issue?
Code Snippet:
// Try initial values
Result[] initials = new Result[] { Result.Zero, Result.One };
foreach (Result initial in initials)
{
BellTest.Run(sim, 1000, initial).Wait();
var res = BellTest.Run(sim, 1000, initial).Result;
var (numZeros, numOnes, agree) = res;
System.Console.WriteLine(
$"Init:{initial,-4} 0s={numZeros,-4} 1s={numOnes,-4} agree={agree,-4}");
}
Every call to a Q# operation is asynchronous, as execution on actual hardware will also be asynchronous, thus every time you call Run you must wait for the execution to complete before you can execute another quantum operation. There are multiple ways to wait for an asynchronous Task to finish, including using the await keyword, the Wait(), or retrieving the Result of the Task.
In this particular case, you invoke BellTest.Run twice, and the second doesn't wait for the first to be completed. The Exeption will go away if you add Wait() to your first Run, for example:
BellTest.Run(sim, 1000, initial).Wait();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With