Ejemplo n.º 1
0
        public override Gate Copy(int referenceBeginRow)
        {
            RegisterPartModel newTarget = copyRegPart(_targetRegister, referenceBeginRow);

            //return new CompositeGate(_name, _gates, newTarget);
            return(new CompositeGate(_name, newTarget));
        }
Ejemplo n.º 2
0
        public void MakeComposite(string name, List <Gate> toGroup)
        {
            Delete();

            RegisterPartModel target = new RegisterPartModel()
            {
                Register = null,
                Offset   = 0,
                Width    = toGroup.Select <Gate, int>(x => x.End).Max() + 1
            };
            CompositeGate cg = new CompositeGate(name, target);

            foreach (StepModel step in _steps)
            {
                for (int j = 0; j < step.Gates.Count; j++)
                {
                    Gate       oldGate = step.Gates[j];
                    CustomGate custom  = oldGate as CustomGate;
                    if (custom != null && custom.FunctionName.Equals(name))
                    {
                        for (int k = oldGate.Begin; k <= oldGate.End; k++)
                        {
                            RegisterRefModel gateRef = GetRefFromOffset(k);
                            Gate             newGate = new EmptyGate(gateRef);
                            step.SetGate(newGate);
                        }
                        Gate replacement = PasteGate(cg, oldGate.Begin);
                        if (step.HasPlace(replacement.Begin, replacement.End))
                        {
                            step.SetGate(replacement);
                        }
                    }
                    j = oldGate.End;
                }
            }

            _compositeGates[name] = toGroup;

            //CompositeGate cg = new CompositeGate(name, toGroup, target);

            Gate toPaste = PasteGate(cg, _selectedItems.Value.BeginRow);

            bool notPasted = true;
            int  i         = _selectedItems.Value.BeginColumn;

            while (notPasted && i <= _selectedItems.Value.EndColumn)
            {
                if (_steps[i].HasPlace(toPaste.Begin, toPaste.End))
                {
                    _steps[i].SetGate(toPaste);
                    notPasted = false;
                }
                i++;
            }
            if (notPasted)
            {
                InsertStepLeft(_selectedItems.Value.BeginColumn);
                _steps[_selectedItems.Value.BeginColumn].SetGate(toPaste);
            }
        }
Ejemplo n.º 3
0
 protected RegisterPartModel incRegPart(RegisterPartModel regRef, RegisterModel register, int afterOffset, int delta = 1)
 {
     if (regRef.Register == register)
     {
         if (regRef.Width + delta == register.Qubits.Count)
         {
             return(new RegisterPartModel()
             {
                 Register = regRef.Register,
                 Offset = regRef.Offset,
                 Width = regRef.Width + delta
             });
         }
         else if (regRef.Offset > afterOffset)
         {
             return(new RegisterPartModel()
             {
                 Register = regRef.Register,
                 Offset = regRef.Offset + delta,
                 Width = regRef.Width
             });
         }
         else if (regRef.Offset + regRef.Width - 1 > afterOffset)
         {
             return(new RegisterPartModel()
             {
                 Register = regRef.Register,
                 Offset = regRef.Offset,
                 Width = regRef.Width + delta
             });
         }
     }
     return(regRef);
 }
Ejemplo n.º 4
0
 protected RegisterPartModel copyRegPart(RegisterPartModel regRef, int referenceBeginRow)
 {
     return(new RegisterPartModel()
     {
         Register = null,
         Offset = regRef.OffsetToRoot - referenceBeginRow,
         Width = regRef.Width
     });
 }
Ejemplo n.º 5
0
        private Gate PasteGate(Gate gate, int referenceBeginRow)
        {
            RegisterRefModel targetRef  = GetRefFromOffset(gate.Target.OffsetToRoot + referenceBeginRow);
            RegisterRefModel?controlRef = null;

            if (gate.Control.HasValue)
            {
                controlRef = GetRefFromOffset(gate.Control.Value.OffsetToRoot + referenceBeginRow);
            }

            switch (gate.Name)
            {
            case GateName.CNot:
                return(new CNotGate(targetRef, controlRef.Value));

            case GateName.Parametric:
                return(PasteParametricGate(gate as ParametricGate, referenceBeginRow));

            case GateName.Composite:
                CompositeGate    cg         = gate as CompositeGate;
                RegisterRefModel partEndRef = GetRefFromOffset(cg.TargetRegister.EndOffsetToRoot + referenceBeginRow);
                if (partEndRef.Register.OffsetToRoot == targetRef.Register.OffsetToRoot)
                {
                    RegisterPartModel target = new RegisterPartModel()
                    {
                        Register = targetRef.Register,
                        Offset   = targetRef.Offset,
                        Width    = cg.TargetRegister.Width
                    };
                    return(new CompositeGate(cg.FunctionName, target));
                }
                else
                {
                    RegisterPartModel target = new RegisterPartModel()
                    {
                        Register = null,
                        Offset   = cg.TargetRegister.Offset + referenceBeginRow,
                        Width    = cg.TargetRegister.Width
                    };
                    return(new CompositeGate(cg.FunctionName, target));
                }

            case GateName.CPhaseShift:
                CPhaseShiftGate    cps      = gate as CPhaseShiftGate;
                RegisterRefModel[] controls = cps.Controls.Select <RegisterRefModel, RegisterRefModel>(x =>
                                                                                                       GetRefFromOffset(x.OffsetToRoot + referenceBeginRow)).ToArray <RegisterRefModel>();
                return(new CPhaseShiftGate(cps.Dist, targetRef, controls));

            case GateName.Empty:
                return(new EmptyGate(targetRef));

            case GateName.Hadamard:
                return(new HadamardGate(targetRef, controlRef));

            case GateName.InvCPhaseShift:
                InvCPhaseShiftGate icps      = gate as InvCPhaseShiftGate;
                RegisterRefModel[] icontrols = icps.Controls.Select <RegisterRefModel, RegisterRefModel>(x =>
                                                                                                         GetRefFromOffset(x.OffsetToRoot + referenceBeginRow)).ToArray <RegisterRefModel>();
                return(new InvCPhaseShiftGate(icps.Dist, targetRef, icontrols));

            case GateName.Measure:
                MeasureGate      mg       = gate as MeasureGate;
                RegisterRefModel beginRef = GetRefFromOffset(mg.Begin + referenceBeginRow);
                RegisterRefModel endRef   = GetRefFromOffset(mg.End + referenceBeginRow);
                return(new MeasureGate(beginRef, endRef));

            case GateName.PhaseKick:
                PhaseKickGate      pk        = gate as PhaseKickGate;
                RegisterRefModel[] controls1 = pk.Controls.Select <RegisterRefModel, RegisterRefModel>(x =>
                                                                                                       GetRefFromOffset(x.OffsetToRoot + referenceBeginRow)).ToArray <RegisterRefModel>();
                return(new PhaseKickGate(pk.Gamma, targetRef, controls1));

            case GateName.PhaseScale:
                PhaseScaleGate ps = gate as PhaseScaleGate;
                return(new PhaseScaleGate(ps.Gamma, targetRef, controlRef));

            case GateName.RotateX:
                RotateXGate rx = gate as RotateXGate;
                return(new RotateXGate(rx.Gamma, targetRef, controlRef));

            case GateName.RotateY:
                RotateYGate ry = gate as RotateYGate;
                return(new RotateYGate(ry.Gamma, targetRef, controlRef));

            case GateName.RotateZ:
                RotateZGate rz = gate as RotateZGate;
                return(new RotateZGate(rz.Gamma, targetRef, controlRef));

            case GateName.SigmaX:
                return(new SigmaXGate(targetRef));

            case GateName.SigmaY:
                return(new SigmaYGate(targetRef, controlRef));

            case GateName.SigmaZ:
                return(new SigmaZGate(targetRef, controlRef));

            case GateName.SqrtX:
                return(new SqrtXGate(targetRef, controlRef));

            case GateName.Toffoli:
                ToffoliGate        t         = gate as ToffoliGate;
                RegisterRefModel[] tcontrols = t.Controls.Select <RegisterRefModel, RegisterRefModel>(x =>
                                                                                                      GetRefFromOffset(x.OffsetToRoot + referenceBeginRow)).ToArray <RegisterRefModel>();
                return(new ToffoliGate(targetRef, tcontrols));

            case GateName.Unitary:
                UnitaryGate u = gate as UnitaryGate;
                return(new UnitaryGate(u.Matrix, targetRef, controlRef));

            default:
                return(null);
            }
        }
Ejemplo n.º 6
0
 public override void IncrementRow(RegisterModel register, int afterOffset, int delta = 1)
 {
     _targetRegister = incRegPart(_targetRegister, register, afterOffset, delta);
 }
Ejemplo n.º 7
0
        //public CompositeGate(string name, List<Gate> gates, RegisterPartModel target)
        //{
        //    _name = name;
        //    _gates = gates;
        //    _targetRegister = target;
        //}

        public CompositeGate(string name, RegisterPartModel target)
        {
            _name           = name;
            _targetRegister = target;
        }