Wednesday, 28 June 2023

Gitlab and Quantum Computing Code

So, I've been looking at Quantum Computing recently and as part of this I thought I'd also take the opportunity to learn GitLab by creating a repository with my scripts.

Gitlab

I setup a GitLab account using the free tier and then created a new project, "quantum"

Setting up SSH

To use SSH keys for communication with GitLab, I followed the instructions in the GitLab Docs to create an SSH key and add to my Profile

For Linux my ssh folder is ./ssh. To generate a new key, run

ssh-keygen -t ed25519 -c "A comment"

And follow the prompts for creating a filename and Passphrase

Then from the GitLab page, click on my Profile image (default for now!) and select Edit Profile -> SSH Keys.

In the Key box paste the contents of the .pub public key I created in the previous step. (Starting with ssh-ed25119), set the Title, usage type of "Authentication & Signing" and finally Add Key.

Visual Studio Code Integration  

From the Visual Studio Code welcome page I selected Clone Git Repository...

I then used the Clone with SSH option from the Clone button to obtain the git url and simply pasted that into Visual Studio Code

Running the script

I have an Anaconda Python environment with cirq added. Details about this will be added later.


Tuesday, 20 June 2023

Setting up Unit Tests in Visual Studio 2022

A very quick blog post tonight. This came about when I wanted to setup a new Unit Test project for an existing .NET 6 solution. The problem came with setting up the project package references and I couldn't remember what to add. So for future reference, the packages I added from NuGet were:

  • NUnit - The main unit-testing framework
  • NUnit3TestAdapter - This is used for running NUnit 3.x tests in Visual Studio
  • Microsoft.NET.Test.SDK - MSBuild targets and properties for building .NET test projects
The project was created as a Class library (Console Application also works but it doesn't really make sense given it's not hosting its own executable)

Add a class and within this attribute the tests to be run with either [Test]

        [Test]
        public void Test_Something()
        {
            var expected = "Something";
            var testValue = DoSomething();
            Assert.That(testValue, Is.EqualTo(expected));
        }

Or [TestCase], if you want to supply parameters

        [TestCase("Test1", 1, 2)]
        [TestCase("Test2", 2, 3)]
        public void LatLong_To_Mercator(string testName, int input, int expected)
        {
            // Run test with parameters and Asset against expected
        }

One last thing, the documentation says to add [TestFixture] to the class but this doesn't seem to be needed. The tests run anyway.

Ahha!
Beginning with NUnit 2.5, the TestFixture attribute is optional for non-parameterized, non-generic fixtures. So long as the class contains at least one method marked with the TestTestCase or TestCaseSource attribute, it will be treated as a test fixture.

Wednesday, 22 February 2023

Avalonia and Cross Platform .NET UI

A big advantage of .NET Core is the ability to write code once that can run on both Windows and Linux. This of course has been done before with Java but for those who use C# this was a huge step forward.

Unfortunately the existing UI Frameworks, Winforms and WPF were not supported on Linux so applications like the SuperMarketPlanner, although they were upgraded to .NET Core, were only able to run on Windows. 

Fortunately though .NET Core, now being open source is building a larger ecosystem and one such addition is Avalonia. This is a modern UI framework that can target both Windows and Linux. 

I initially played around before deciding to follow the excellent tutorial to build a Music Store App.

The framework is similar to WPF. We have a markup file to describe how the UI components are arranged, the View. A ViewModel is then setup to define how to bind the view to properties and commands.

As an example I setup a simple test that used a button to load and display an image. The view below shows the Button and Image elements, with the Image Bound to the Photo property:

<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="using:AvaloniaTestApp.ViewModels"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="AvaloniaTestApp.Views.MainWindow"
Icon="/Assets/avalonia-logo.ico"
Title="AvaloniaTestApp">
<Design.DataContext>
<vm:MainWindowViewModel/>
</Design.DataContext>
<StackPanel>
<Button Content="Load Image" Command="{Binding LoadImageCommand}" HorizontalAlignment="Right" VerticalAlignment="Top" />
<Image Stretch="Uniform" Width="400" Source="{Binding Image}"/>
</StackPanel>
</Window>


And the ViewModel code sets up the property to the bitmap. There is a method to load the image from a file that's called from a button command (see the Music Store App demo for how to setup the command)

private Bitmap? _image;
// Property that's bound to the View
public Bitmap? Image
{
get { return _image; }
private set => this.RaiseAndSetIfChanged(ref _image, value);
}
// This can be called for example from a button command
public async Task LoadImage()
{
await using (var imageStream = new FileStream(@"Path\To\Image", FileMode.Open))
{
Image = await Task.Run(()=> Bitmap.DecodeToWidth(imageStream, _imageWidth));
}
}


And here is the application running on Windows...


And Ubuntu...