Page 38 - MSDN Magazine, September 2019
P. 38

Figure 4 Creating Entangled Qubits in C#
QuantumSimulator has an internal IoC container that allows you to retrieve various objects that are commonly used by its generated C# code. For example, within a C# file you can use the Get<T> method of the QuantumSimulator object to retrieve an object for resetting a qubit’s state and releasing a qubit, performing the same action as a using block in Q#:
resetOperation = simulator.Get<ICallable<Qubit, QVoid>>(typeof(Reset)); releaseOperation = simulator.Get<Release>(typeof(Release));
To allocate a new Qubit, you use the Microsoft.Quantum.Intrin- sic.Allocate object. You also need the CNOT and H (Hadamard) gate objects, which are retrieved from the container, like so:
allocator = simulator.Get<Allocate>(typeof(Allocate));
cnotGate = simulator.Get<IUnitary<(Qubit, Qubit)>>(typeof(CNOT)); hGate = simulator.Get<IUnitary<Qubit>>(typeof(H));
With these objects you can generate entangled pairs of qubits. The GetEntangledPairsAsync method of the QOperations class creates a list of Qubit tuples (see Figure 4).
I use the Apply method of the Allocate object to retrieve two free qubits from the simulator. I apply the Hadamard gate to the first qubit, and then the CNOT to both, placing them in an entangled state. There’s no need to reset and release them immediately; I’m able to return the Qubit pairs from the method. Qubits still need to be released—otherwise the application would quickly run out of memory. I just postpone that step.
Qubits are released with the ReleaseQubitsAsync method, as shown in Figure 5.
public Task<IList<(Qubit, Qubit)>> GetEntangledPairsAsync(int count) {
IList<(Qubit, Qubit)> result = new List<(Qubit, Qubit)>(count);
for (int i = 0; i < count; i++) {
var qubits = allocator.Apply(2); hGate.Apply(qubits[0]); cnotGate.Apply((qubits[0], qubits[1])); result.Add((qubits[0], qubits[1]));
}
return Task.FromResult(result); }
Figure 5 Releasing Qubits in C#
public Task ReleaseQubitsAsync(IEnumerable<Qubit> qubits) {
foreach (Qubit qubit in qubits) {
resetOperation.Apply(qubit); releaseOperation.Apply(qubit);
/* Alternatively, we could do: */
// simulator.QubitManager.Release(qubit);
}
return Task.CompletedTask; }
of restricting myself to Q# for all quantum activities, I use C# for allocating, entangling and deallocating qubits.
The QOperations class creates an instance of the QuantumSim-
ulator class, which is located in the Microsoft.Quantum.Simula-
tion.Simulators namespace. QuantumSimulator provides an API
for manipulating the native simulator component. The simulator
isinstalledwiththeMicrosoftQuantumDevelopmentKit. Thatcompletesthequantumelementofthisarticle.Nowlet’slookat
Figure 6 Alice, Bob and Charlie Razor Pages 30 msdn magazine
Blazor and at implementing a server- side Blazor project with MVVM.
Using the MVVM
Pattern with Blazor
I’ve been a longtime fan of XAML. I like the way I can write view-model logic in C# and combine it with a designer-friendly reusable layout file glued together with data bindings. Blazor allows the same approach, but instead of XAML it uses Razor, which has a concise syntax for marking up dynamic content within HTML.
If you’re new to Blazor, see the installation instructions at tinyurl.com/ installblazor for your IDE of choice.
I first implemented this project using Universal Windows Platform (UWP). UWP is compatible with .NET Core and it made sense to rapid- ly build out the UI in a XAML-based technology. In fact, the UWP version of the application is located in the
Quantum Computing
For each supplied Qubit, I reset the qubit using the Reset object’s Apply method. I then inform the simulator that the qubit is free using the Release object’s Apply method. Alternatively, you could use the simulator’s QubitManager property to release the qubit.


































































































   36   37   38   39   40