Uh oh!
There was an error while loading. Please reload this page.
forked from microsoft/Quantum
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
Latest commit
100 lines (91 loc) · 4.35 KB
/
Copy pathProgram.cs
File metadata and controls
100 lines (91 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
usingSystem;
usingMicrosoft.Quantum.Simulation.Simulators;
usingMicrosoft.Quantum.Simulation.Core;
namespaceMicrosoft.Quantum.Samples.IntegerFactorization
{
/// <summary>
/// This is a Console program that runs Shor's algorithm
/// on a Quantum Simulator.
/// </summary>
classProgram
{
// The console application takes up to three arguments
// 1. numberToFactor -- number to be factored
// 2. nTrials -- number of trial to perform
// 3. useRobustPhaseEstimation -- if true uses Robust Phase Estimation,
// uses Quantum Phase Estimation otherwise.
// If you build the Debug configuration, the executable will be located in
// Libraries\Samples\IntegerFactorization\bin\Debug\ folder;
// for the Release configuration the folder is
// Libraries\Samples\IntegerFactorization\bin\Release.
// The name of the executable is IntegerFactorization.exe.
staticvoidMain(string[]args)
{
// Default values used if no arguments are provided
longnumberToFactor=15;
longnTrials=100;
booluseRobustPhaseEstimation=true;
// Parse the arguments provided in command line
if(args.Length>=1)
{
// The first argument is the number to factor
Int64.TryParse(args[0],outnumberToFactor);
}
if(args.Length>=2)
{
// The second is the number of trials
Int64.TryParse(args[1],outnTrials);
}
if(args.Length>=3)
{
// The third argument indicates if Robust or Quantum Phase Estimation
// should be used
bool.TryParse(args[2],outuseRobustPhaseEstimation);
}
// Repeat Shor's algorithm multiple times as the algorithm is
// probabilistic and there are several ways that it can fail.
for(inti=0;i<nTrials;++i)
{
try
{
// Make sure to use simulator within using block.
// This ensures that all resources used by QuantumSimulator
// are properly released if the algorithm fails and throws an exception.
using(QuantumSimulatorsim=newQuantumSimulator())
{
// Report the number being factored to the standard output
Console.WriteLine($"==========================================");
Console.WriteLine($"Factoring {numberToFactor}");
// Compute the factors
(longfactor1,longfactor2)=
Shor.Run(sim,numberToFactor,useRobustPhaseEstimation).Result;
Console.WriteLine($"Factors are {factor1} and {factor2}");
}
}
// Shor's algorithm is a probabilistic algorithm and can fail with certain
// probability in several ways. For more details see Shor.qs.
// If the run of Shor's algorithm fails it throws ExecutionFailException.
// However, due to the use of System.Task in .Run method,
// the exception of interest is getting wrapped into AggregateException.
catch(AggregateExceptione)
{
// Report the failure of the algorithm to standard output
Console.WriteLine($"This run of Shor's algorithm failed:");
// Unwrap AggregateException to get the message from Q# fail statement.
// Go through all inner exceptions.
foreach(ExceptioneInnerine.InnerExceptions)
{
// If the exception of type ExecutionFailException
if(eInnerisExecutionFailExceptionfailException)
{
// Print the message it contains
Console.WriteLine($" {failException.Message}");
}
}
}
}
}
}
}