This repository has been archived by the owner on Jan 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
ConfigurationSourceTests.cs
103 lines (91 loc) · 4.85 KB
/
ConfigurationSourceTests.cs
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
101
102
103
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Quantum.IQSharp;
using Microsoft.Quantum.IQSharp.Common;
using Microsoft.Quantum.IQSharp.Jupyter;
using Microsoft.Quantum.Simulation.Core;
using Microsoft.Quantum.Simulation.Simulators;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Tests.IQSharp
{
[TestClass]
public class ConfigurationSourceTests
{
[TestMethod]
public void Persists()
{
static void DeleteConfig()
{
if (File.Exists(ConfigurationSource.ConfigPath))
{
File.Delete(ConfigurationSource.ConfigPath);
}
Assert.IsFalse(File.Exists(ConfigurationSource.ConfigPath));
};
DeleteConfig();
var config = new ConfigurationSource() as IConfigurationSource;
try
{
Assert.IsNotNull(config);
Assert.IsFalse(File.Exists(ConfigurationSource.ConfigPath)); // Make sure the file is not created automatically.
Assert.AreEqual(CommonNativeSimulator.BasisStateLabelingConvention.LittleEndian, config.BasisStateLabelingConvention);
Assert.AreEqual(false, config.TruncateSmallAmplitudes);
Assert.AreEqual(4, config.MeasurementDisplayPrecision);
Assert.AreEqual("mixed", config.NoisySimulatorRepresentation);
// Change some values
config.Configuration["dump.basisStateLabelingConvention"] = JToken.Parse("\"BigEndian\"");
config.Configuration["dump.truncateSmallAmplitudes"] = JToken.Parse("true");
config.Configuration["dump.measurementDisplayPrecision"] = JToken.Parse("2");
config.Configuration["simulators.noisy.representation"] = JToken.Parse("\"stabilizer\"");
// Save:
config.Persist();
Assert.IsTrue(File.Exists(ConfigurationSource.ConfigPath));
config = new ConfigurationSource() as IConfigurationSource;
Assert.IsNotNull(config);
Assert.AreEqual(CommonNativeSimulator.BasisStateLabelingConvention.BigEndian, config.BasisStateLabelingConvention);
Assert.AreEqual(true, config.TruncateSmallAmplitudes);
Assert.AreEqual(2, config.MeasurementDisplayPrecision);
Assert.AreEqual("stabilizer", config.NoisySimulatorRepresentation);
Assert.AreEqual(MeasurementDisplayStyle.BarAndNumber, config.MeasurementDisplayStyle);
}
finally
{
config.Configuration["simulators.noisy.representation"] = JToken.Parse("\"mixed\"");
DeleteConfig();
}
}
[TestMethod]
public void ReadFromEnvironment()
{
var config = new ConfigurationSource(skipLoading: true) as IConfigurationSource;
Assert.IsNotNull(config);
Assert.AreEqual(CommonNativeSimulator.BasisStateLabelingConvention.LittleEndian, config.BasisStateLabelingConvention);
Assert.AreEqual(false, config.TruncateSmallAmplitudes);
Assert.AreEqual(4, config.MeasurementDisplayPrecision);
Assert.AreEqual("mixed", config.NoisySimulatorRepresentation);
// Set values via environment:
System.Environment.SetEnvironmentVariable("DUMP_BASISSTATELABELINGCONVENTION", "BigEndian");
System.Environment.SetEnvironmentVariable("IQSHARP_DUMP_TRUNCATESMALLAMPLITUDES", "true");
System.Environment.SetEnvironmentVariable("DUMP_MEASUREMENTDISPLAYPRECISION", "2");
System.Environment.SetEnvironmentVariable("IQSHARP_SIMULATORS_NOISY_REPRESENTATION", "stabilizer");
// Read values again, environment should be reflected:
Assert.AreEqual(CommonNativeSimulator.BasisStateLabelingConvention.BigEndian, config.BasisStateLabelingConvention);
Assert.AreEqual(true, config.TruncateSmallAmplitudes);
Assert.AreEqual(2, config.MeasurementDisplayPrecision);
Assert.AreEqual("stabilizer", config.NoisySimulatorRepresentation);
Assert.AreEqual(MeasurementDisplayStyle.BarAndNumber, config.MeasurementDisplayStyle);
// Reset environment:
System.Environment.SetEnvironmentVariable("DUMP_BASISSTATELABELINGCONVENTION", null);
System.Environment.SetEnvironmentVariable("IQSHARP_DUMP_TRUNCATESMALLAMPLITUDES", null);
System.Environment.SetEnvironmentVariable("DUMP_MEASUREMENTDISPLAYPRECISION", null);
System.Environment.SetEnvironmentVariable("IQSHARP_EXPERIMENTAL_SIMULATORS_REPRESENTATION", null);
}
}
}