Exemplo n.º 1
0
 public override void Write(Utf8JsonWriter writer, ChpOperation value, JsonSerializerOptions options)
 {
     writer.WriteStartObject();
     writer.WritePropertyName(
         value switch
     {
         ChpOperation.Cnot _ => "Cnot",
         ChpOperation.Hadamard _ => "Hadamard",
         ChpOperation.Phase _ => "Phase",
         ChpOperation.AdjointPhase _ => "AdjointPhase",
         _ => throw new JsonException()
     }
Exemplo n.º 2
0
        public override ChpOperation Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            reader.Require(JsonTokenType.StartObject, read: false);
            reader.Require(JsonTokenType.PropertyName);
            ChpOperation?operation = null;
            ulong        idx;

            switch (reader.GetString())
            {
            case "Cnot":
                var idxs = JsonSerializer.Deserialize <List <ulong> >(ref reader);
                operation = new ChpOperation.Cnot
                {
                    IdxControl = idxs[0],
                    IdxTarget  = idxs[1]
                };
                break;

            case "Hadamard":
                reader.Read();
                idx       = reader.GetUInt64();
                operation = new ChpOperation.Hadamard
                {
                    IdxTarget = idx
                };
                break;

            case "Phase":
                reader.Read();
                idx       = reader.GetUInt64();
                operation = new ChpOperation.Phase
                {
                    IdxTarget = idx
                };
                break;

            case "AdjointPhase":
                reader.Read();
                idx       = reader.GetUInt64();
                operation = new ChpOperation.AdjointPhase
                {
                    IdxTarget = idx
                };
                break;
            }
            if (operation == null)
            {
                throw new JsonException("Did not read an operation.");
            }
            reader.Require(JsonTokenType.EndObject);
            return(operation);
        }