示例#1
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);
            }
        }
示例#2
0
        public void DeleteRegister(int registerIndex)
        {
            if (_currentStep != 0)
            {
                return;
            }

            SelectedItems = null;

            RegisterModel reg       = _registers[registerIndex];
            int           regBegin  = reg.OffsetToRoot;
            int           regEnd    = reg.OffsetToRoot + reg.Qubits.Count - 1;
            int           regLength = reg.Qubits.Count;

            foreach (StepModel step in Steps)
            {
                Gate firstOld = step.Gates[regBegin];
                if (firstOld.Begin < regBegin)
                {
                    for (int j = firstOld.Begin; j <= firstOld.End; j++)
                    {
                        RegisterRefModel gateRef = GetRefFromOffset(j);
                        Gate             newGate = new EmptyGate(gateRef);
                        step.SetGate(newGate);
                    }
                }
                Gate lastOld = step.Gates[regEnd];
                if (lastOld.End > regEnd)
                {
                    for (int j = lastOld.Begin; j <= lastOld.End; j++)
                    {
                        RegisterRefModel gateRef = GetRefFromOffset(j);
                        Gate             newGate = new EmptyGate(gateRef);
                        step.SetGate(newGate);
                    }
                }

                for (int i = 0; i < regLength; i++)
                {
                    step.Gates.RemoveAt(regBegin);
                }
            }

            int k = 0;

            for (; k < registerIndex; k++)
            {
                _registers[k].IncrementOffset(-regLength);
            }
            k++;
            for (; k < _registers.Count; k++)
            {
                _registers[k].UpdateIndex(k - 1);
            }

            _registers.RemoveAt(registerIndex);
        }
示例#3
0
        public void DeleteQubit(int registerIndex, int row)
        {
            if (_currentStep != 0)
            {
                return;
            }

            SelectedItems = null;

            RegisterModel reg          = _registers[registerIndex];
            int           offsetToRoot = reg.OffsetToRoot + row;

            foreach (StepModel step in Steps)
            {
                Gate oldGate = step.Gates[offsetToRoot];
                if (oldGate.Name != GateName.Empty)
                {
                    for (int j = oldGate.Begin; j <= oldGate.End; j++)
                    {
                        RegisterRefModel gateRef = GetRefFromOffset(j);
                        Gate             newGate = new EmptyGate(gateRef);
                        step.SetGate(newGate);
                    }
                }

                for (int i = offsetToRoot + 1; i < reg.OffsetToRoot + reg.Qubits.Count; i++)
                {
                    step.Gates[i].IncrementRow(reg, row, -1);
                    i = step.Gates[i].End + 1;
                }

                //step.Gates.RemoveAt(offsetToRoot);
            }

            reg.Qubits.RemoveAt(row);

            for (int i = 0; i < registerIndex; i++)
            {
                _registers[i].IncrementOffset(-1);
            }

            foreach (StepModel step in Steps)
            {
                step.Gates.RemoveAt(offsetToRoot);
            }
        }
示例#4
0
        private void CutCopy(bool cut)
        {
            if (_selectedItems.HasValue)
            {
                _clipboard = new List <List <Gate> >();

                _clipboardSelection = new Selection(
                    0,
                    _selectedItems.Value.RowSpan - 1,
                    0,
                    _selectedItems.Value.ColumnSpan - 1);

                for (int i = _selectedItems.Value.BeginColumn; i <= _selectedItems.Value.EndColumn; i++)
                {
                    List <Gate> current = new List <Gate>();
                    _clipboard.Add(current);

                    for (int j = _selectedItems.Value.BeginRow; j <= _selectedItems.Value.EndRow; j++)
                    {
                        if ((_steps[i].Gates[j].Begin >= _selectedItems.Value.BeginRow) &&
                            (_steps[i].Gates[j].End <= _selectedItems.Value.EndRow))
                        {
                            Gate oldGate = _steps[i].Gates[j];

                            current.Add(oldGate.Copy(_selectedItems.Value.BeginRow));

                            if (cut && oldGate.Name != GateName.Empty)
                            {
                                for (; j <= oldGate.End; j++)
                                {
                                    RegisterRefModel gateRef = GetRefFromOffset(j);
                                    Gate             newGate = new EmptyGate(gateRef);
                                    _steps[i].SetGate(newGate);
                                }
                            }
                            j = oldGate.End;
                        }
                    }
                }
            }
        }
示例#5
0
        public void SetGate(int pressedColumn, RegisterRefModel pressedRow, DragDropKeyStates keyStates)
        {
            ActionName action = HomeVM.SelectedAction;
            Gate oldGate;

            // make selection
            if (keyStates.HasFlag(DragDropKeyStates.ShiftKey))
            {
                // move selection
                if (keyStates.HasFlag(DragDropKeyStates.ControlKey))
                {
                    if (_model.IsSelected(pressedRow.OffsetToRoot, pressedColumn))
                    {
                        _model.Cut();
                        _model.Select(_row.OffsetToRoot, _row.OffsetToRoot, _column, _column);
                        _model.Paste();
                    }
                }
                else
                {
                    _model.Select(pressedRow.OffsetToRoot, _row.OffsetToRoot, pressedColumn, _column);
                }
            }
            else
            {
                switch (action)
                {
                    case ActionName.Empty:
                        oldGate = _model.Steps[_column].Gates[_row.OffsetToRoot];
                        if (oldGate.Name != GateName.Empty)
                        {
                            for (int i = oldGate.Begin; i <= oldGate.End; i++)
                            {
                                RegisterRefModel gateRef = _model.GetRefFromOffset(i);
                                Gate newGate = new EmptyGate(gateRef);
                                _model.Steps[_column].SetGate(newGate);
                            }

                            if (oldGate is MultiControlledGate)
                            {
                                MultiControlledGate mcg = oldGate as MultiControlledGate;
                                if (mcg.Controls.Contains<RegisterRefModel>(_row))
                                {
                                    RegisterRefModel[] toRemove = new RegisterRefModel[] { _row };
                                    RegisterRefModel[] newControls = mcg.Controls.Except<RegisterRefModel>(toRemove).ToArray();
                                    Gate toAdd = null;
                                    if (oldGate.Name == GateName.PhaseKick)
                                    {
                                        PhaseKickGate pk = oldGate as PhaseKickGate;
                                        toAdd = new PhaseKickGate(pk.Gamma, pk.Target, newControls);
                                    }
                                    else if (oldGate.Name == GateName.CPhaseShift)
                                    {
                                        CPhaseShiftGate cps = oldGate as CPhaseShiftGate;
                                        toAdd = new CPhaseShiftGate(cps.Dist, cps.Target, newControls);
                                    }
                                    else if (oldGate.Name == GateName.InvCPhaseShift)
                                    {
                                        InvCPhaseShiftGate icps = oldGate as InvCPhaseShiftGate;
                                        toAdd = new InvCPhaseShiftGate(icps.Dist, icps.Target, newControls);
                                    }
                                    else // Toffoli
                                    {
                                        if (newControls.Length > 1)
                                        {
                                            toAdd = new ToffoliGate(oldGate.Target, newControls);
                                        }
                                        else
                                        {
                                            toAdd = new CNotGate(oldGate.Target, newControls[0]);
                                        }
                                    }
                                    _model.Steps[_column].SetGate(toAdd);
                                }
                            }
                            else if (oldGate.Name == GateName.CNot)
                            {
                                if (oldGate.Control.Value.OffsetToRoot == _row.OffsetToRoot)
                                {
                                    Gate toAdd = new SigmaXGate(oldGate.Target);
                                    _model.Steps[_column].SetGate(toAdd);
                                }
                            }
                            else if (oldGate is SingleGate)
                            {
                                SingleGate sg = oldGate as SingleGate;
                                if (sg.Control.HasValue)
                                {
                                    if (sg.Control.Value.OffsetToRoot == _row.OffsetToRoot)
                                    {
                                        Gate toAdd = null;
                                        switch (sg.Name)
                                        {
                                            case GateName.Hadamard:
                                                toAdd = new HadamardGate(sg.Target);
                                                break;
                                            case GateName.PhaseScale:
                                                toAdd = new PhaseScaleGate((sg as PhaseScaleGate).Gamma, sg.Target);
                                                break;
                                            case GateName.RotateX:
                                                toAdd = new RotateXGate((sg as RotateXGate).Gamma, sg.Target);
                                                break;
                                            case GateName.RotateY:
                                                toAdd = new RotateYGate((sg as RotateYGate).Gamma, sg.Target);
                                                break;
                                            case GateName.RotateZ:
                                                toAdd = new RotateZGate((sg as RotateZGate).Gamma, sg.Target);
                                                break;
                                            case GateName.SigmaX:
                                                toAdd = new SigmaXGate(sg.Target);
                                                break;
                                            case GateName.SigmaY:
                                                toAdd = new SigmaYGate(sg.Target);
                                                break;
                                            case GateName.SigmaZ:
                                                toAdd = new SigmaZGate(sg.Target);
                                                break;
                                            case GateName.SqrtX:
                                                toAdd = new SqrtXGate(sg.Target);
                                                break;
                                            case GateName.Unitary:
                                                toAdd = new UnitaryGate((sg as UnitaryGate).Matrix, sg.Target);
                                                break;
                                            default:
                                                break;
                                        }
                                        _model.Steps[_column].SetGate(toAdd);
                                    }
                                }
                            }
                        }
                        break;
                    case ActionName.Hadamard:
                        oldGate = _model.Steps[_column].Gates[_row.OffsetToRoot];
                        if (oldGate.Name == GateName.Empty)
                        {
                            _model.Steps[_column].SetGate(new HadamardGate(_row));
                            _model.AddStepAfter(_column);
                        }
                        break;
                    case ActionName.SigmaX:
                        oldGate = _model.Steps[_column].Gates[_row.OffsetToRoot];
                        if (oldGate.Name == GateName.Empty)
                        {
                            _model.Steps[_column].SetGate(new SigmaXGate(_row));
                            _model.AddStepAfter(_column);
                        }
                        break;
                    case ActionName.SigmaY:
                        oldGate = _model.Steps[_column].Gates[_row.OffsetToRoot];
                        if (oldGate.Name == GateName.Empty)
                        {
                            _model.Steps[_column].SetGate(new SigmaYGate(_row));
                            _model.AddStepAfter(_column);
                        }
                        break;
                    case ActionName.SigmaZ:
                        oldGate = _model.Steps[_column].Gates[_row.OffsetToRoot];
                        if (oldGate.Name == GateName.Empty)
                        {
                            _model.Steps[_column].SetGate(new SigmaZGate(_row));
                            _model.AddStepAfter(_column);
                        }
                        break;
                    case ActionName.SqrtX:
                        oldGate = _model.Steps[_column].Gates[_row.OffsetToRoot];
                        if (oldGate.Name == GateName.Empty)
                        {
                            _model.Steps[_column].SetGate(new SqrtXGate(_row));
                            _model.AddStepAfter(_column);
                        }
                        break;
                    case ActionName.RotateX:
                    case ActionName.RotateY:
                    case ActionName.RotateZ:
                    case ActionName.PhaseKick:
                    case ActionName.PhaseScale:
                        oldGate = _model.Steps[_column].Gates[_row.OffsetToRoot];
                        if (oldGate.Name == GateName.Empty)
                        {
                            MainWindow window = App.Current.MainWindow as MainWindow;
                            GammaInputVM gammmaVM = new GammaInputVM();
                            ICustomContentDialog dialog = window.DialogManager.CreateCustomContentDialog(new GammaInput(gammmaVM), DialogMode.OkCancel);
                            dialog.Ok = () =>
                            {
                                double gamma = gammmaVM.Gamma;
                                if (action == ActionName.RotateX)
                                {
                                    _model.Steps[_column].SetGate(new RotateXGate(gamma, _row));
                                    _model.AddStepAfter(_column);
                                }
                                else if (action == ActionName.RotateY)
                                {
                                    _model.Steps[_column].SetGate(new RotateYGate(gamma, _row));
                                    _model.AddStepAfter(_column);
                                }
                                else if (action == ActionName.RotateZ)
                                {
                                    _model.Steps[_column].SetGate(new RotateZGate(gamma, _row));
                                    _model.AddStepAfter(_column);
                                }
                                else if (action == ActionName.PhaseKick)
                                {
                                    _model.Steps[_column].SetGate(new PhaseKickGate(gamma, _row));
                                    _model.AddStepAfter(_column);
                                }
                                else
                                {
                                    _model.Steps[_column].SetGate(new PhaseScaleGate(gamma, _row));
                                    _model.AddStepAfter(_column);
                                }
                            };
                            dialog.Show();
                        }
                        break;
                    case ActionName.Unitary:
                        oldGate = _model.Steps[_column].Gates[_row.OffsetToRoot];
                        if (oldGate.Name == GateName.Empty)
                        {
                            MainWindow window1 = App.Current.MainWindow as MainWindow;
                            MatrixInputVM matrixVM = new MatrixInputVM();
                            ICustomContentDialog dialog1 = window1.DialogManager.CreateCustomContentDialog(new MatrixInput(matrixVM), DialogMode.OkCancel);
                            dialog1.Ok = () =>
                            {
                                Complex[,] matrix = matrixVM.Matrix;
                                if (matrix != null)
                                {
                                    _model.Steps[_column].SetGate(new UnitaryGate(matrix, _row));
                                    _model.AddStepAfter(_column);
                                }
                            };
                            dialog1.Show();
                        }
                        break;
                    case ActionName.Control:
                        if (pressedColumn == _column)
                        {
                            oldGate = _model.Steps[_column].Gates[_row.OffsetToRoot];
                            if (!_row.Equals(pressedRow))
                            {
                                if (oldGate.Name == GateName.CNot)
                                {
                                    CNotGate oldCnot = oldGate as CNotGate;
                                    if (pressedRow.OffsetToRoot < oldGate.Begin)
                                    {
                                        if (_model.Steps[_column].HasPlace(pressedRow.OffsetToRoot, oldGate.Begin - 1))
                                        {
                                            _model.Steps[_column].SetGate(new ToffoliGate(oldCnot.Target, oldCnot.Control.Value, pressedRow));
                                        }
                                    }
                                    else if (pressedRow.OffsetToRoot > oldGate.End)
                                    {
                                        if (_model.Steps[_column].HasPlace(oldGate.End + 1, pressedRow.OffsetToRoot))
                                        {
                                            _model.Steps[_column].SetGate(new ToffoliGate(oldCnot.Target, oldCnot.Control.Value, pressedRow));
                                        }
                                    }
                                    else // new Control inside
                                    {
                                        //check if not doubled
                                        if (_row.OffsetToRoot != oldCnot.Target.OffsetToRoot &&
                                            _row.OffsetToRoot != oldCnot.Control.Value.OffsetToRoot)
                                        {
                                            _model.Steps[_column].SetGate(new ToffoliGate(oldCnot.Target, oldCnot.Control.Value, _row));
                                        }
                                    }
                                }
                                else if (oldGate.Name == GateName.Toffoli)
                                {
                                    ToffoliGate oldT = oldGate as ToffoliGate;
                                    if (pressedRow.OffsetToRoot < oldGate.Begin)
                                    {
                                        if (_model.Steps[_column].HasPlace(pressedRow.OffsetToRoot, oldGate.Begin - 1))
                                        {
                                            _model.Steps[_column].SetGate(new ToffoliGate(oldT.Target, pressedRow, oldT.Controls));
                                        }
                                    }
                                    else if (pressedRow.OffsetToRoot > oldGate.End)
                                    {
                                        if (_model.Steps[_column].HasPlace(oldGate.End + 1, pressedRow.OffsetToRoot))
                                        {
                                            _model.Steps[_column].SetGate(new ToffoliGate(oldT.Target, pressedRow, oldT.Controls));
                                        }
                                    }
                                    else // new Control inside
                                    {
                                        //check if not doubled
                                        if (_row.OffsetToRoot != oldT.Target.OffsetToRoot &&
                                            !oldT.Controls.Contains<RegisterRefModel>(_row))
                                        {
                                            _model.Steps[_column].SetGate(new ToffoliGate(oldT.Target, _row, oldT.Controls));
                                        }
                                    }
                                }
                                else if (oldGate.Name == GateName.CPhaseShift)
                                {
                                    CPhaseShiftGate oldT = oldGate as CPhaseShiftGate;
                                    if (pressedRow.OffsetToRoot < oldGate.Begin)
                                    {
                                        if (_model.Steps[_column].HasPlace(pressedRow.OffsetToRoot, oldGate.Begin - 1))
                                        {
                                            List<RegisterRefModel> cList = oldT.Controls.ToList<RegisterRefModel>();
                                            cList.Add(pressedRow);
                                            RegisterRefModel[] cParams = cList.ToArray<RegisterRefModel>();
                                            _model.Steps[_column].SetGate(new CPhaseShiftGate(oldT.Dist, oldT.Target, cParams));
                                        }
                                    }
                                    else if (pressedRow.OffsetToRoot > oldGate.End)
                                    {
                                        if (_model.Steps[_column].HasPlace(oldGate.End + 1, pressedRow.OffsetToRoot))
                                        {
                                            List<RegisterRefModel> cList = oldT.Controls.ToList<RegisterRefModel>();
                                            cList.Add(pressedRow);
                                            RegisterRefModel[] cParams = cList.ToArray<RegisterRefModel>();
                                            _model.Steps[_column].SetGate(new CPhaseShiftGate(oldT.Dist, oldT.Target, cParams));
                                        }
                                    }
                                    else // new Control inside
                                    {
                                        List<RegisterRefModel> cList = oldT.Controls.ToList<RegisterRefModel>();
                                        cList.Add(_row);
                                        RegisterRefModel[] cParams = cList.ToArray<RegisterRefModel>();
                                        _model.Steps[_column].SetGate(new CPhaseShiftGate(oldT.Dist, oldT.Target, cParams));
                                    }
                                }
                                else if (oldGate.Name == GateName.InvCPhaseShift)
                                {
                                    InvCPhaseShiftGate oldT = oldGate as InvCPhaseShiftGate;
                                    if (pressedRow.OffsetToRoot < oldGate.Begin)
                                    {
                                        if (_model.Steps[_column].HasPlace(pressedRow.OffsetToRoot, oldGate.Begin - 1))
                                        {
                                            List<RegisterRefModel> cList = oldT.Controls.ToList<RegisterRefModel>();
                                            cList.Add(pressedRow);
                                            RegisterRefModel[] cParams = cList.ToArray<RegisterRefModel>();
                                            _model.Steps[_column].SetGate(new InvCPhaseShiftGate(oldT.Dist, oldT.Target, cParams));
                                        }
                                    }
                                    else if (pressedRow.OffsetToRoot > oldGate.End)
                                    {
                                        if (_model.Steps[_column].HasPlace(oldGate.End + 1, pressedRow.OffsetToRoot))
                                        {
                                            List<RegisterRefModel> cList = oldT.Controls.ToList<RegisterRefModel>();
                                            cList.Add(pressedRow);
                                            RegisterRefModel[] cParams = cList.ToArray<RegisterRefModel>();
                                            _model.Steps[_column].SetGate(new InvCPhaseShiftGate(oldT.Dist, oldT.Target, cParams));
                                        }
                                    }
                                    else // new Control inside
                                    {
                                        List<RegisterRefModel> cList = oldT.Controls.ToList<RegisterRefModel>();
                                        cList.Add(_row);
                                        RegisterRefModel[] cParams = cList.ToArray<RegisterRefModel>();
                                        _model.Steps[_column].SetGate(new InvCPhaseShiftGate(oldT.Dist, oldT.Target, cParams));
                                    }
                                }
                                else if (oldGate.Name == GateName.PhaseKick)
                                {
                                    PhaseKickGate oldT = oldGate as PhaseKickGate;
                                    if (pressedRow.OffsetToRoot < oldGate.Begin)
                                    {
                                        if (_model.Steps[_column].HasPlace(pressedRow.OffsetToRoot, oldGate.Begin - 1))
                                        {
                                            List<RegisterRefModel> cList = oldT.Controls.ToList<RegisterRefModel>();
                                            cList.Add(pressedRow);
                                            RegisterRefModel[] cParams = cList.ToArray<RegisterRefModel>();
                                            _model.Steps[_column].SetGate(new PhaseKickGate(oldT.Gamma, oldT.Target, cParams));
                                        }
                                    }
                                    else if (pressedRow.OffsetToRoot > oldGate.End)
                                    {
                                        if (_model.Steps[_column].HasPlace(oldGate.End + 1, pressedRow.OffsetToRoot))
                                        {
                                            List<RegisterRefModel> cList = oldT.Controls.ToList<RegisterRefModel>();
                                            cList.Add(pressedRow);
                                            RegisterRefModel[] cParams = cList.ToArray<RegisterRefModel>();
                                            _model.Steps[_column].SetGate(new PhaseKickGate(oldT.Gamma, oldT.Target, cParams));
                                        }
                                    }
                                    else // new Control inside
                                    {
                                        List<RegisterRefModel> cList = oldT.Controls.ToList<RegisterRefModel>();
                                        cList.Add(_row);
                                        RegisterRefModel[] cParams = cList.ToArray<RegisterRefModel>();
                                        _model.Steps[_column].SetGate(new PhaseKickGate(oldT.Gamma, oldT.Target, cParams));
                                    }
                                }
                                else if (oldGate.Control == null)
                                {
                                    int lastEmptyRow = _row.OffsetToRoot - 1;
                                    if (pressedRow.OffsetToRoot > _row.OffsetToRoot)
                                    {
                                        lastEmptyRow = lastEmptyRow + 2;
                                    }
                                    if (_model.Steps[_column].HasPlace(pressedRow.OffsetToRoot, lastEmptyRow))
                                    {
                                        switch (oldGate.Name)
                                        {
                                            case GateName.Hadamard:
                                                _model.Steps[_column].SetGate(new HadamardGate(_row, pressedRow));
                                                break;
                                            case GateName.PhaseScale:
                                                PhaseScaleGate oldPs = oldGate as PhaseScaleGate;
                                                _model.Steps[_column].SetGate(new PhaseScaleGate(oldPs.Gamma, _row, pressedRow));
                                                break;
                                            case GateName.RotateX:
                                                RotateXGate oldRx = oldGate as RotateXGate;
                                                _model.Steps[_column].SetGate(new RotateXGate(oldRx.Gamma, _row, pressedRow));
                                                break;
                                            case GateName.RotateY:
                                                RotateYGate oldRy = oldGate as RotateYGate;
                                                _model.Steps[_column].SetGate(new RotateYGate(oldRy.Gamma, _row, pressedRow));
                                                break;
                                            case GateName.RotateZ:
                                                RotateZGate oldRz = oldGate as RotateZGate;
                                                _model.Steps[_column].SetGate(new RotateZGate(oldRz.Gamma, _row, pressedRow));
                                                break;
                                            case GateName.SigmaX:
                                                _model.Steps[_column].SetGate(new CNotGate(_row, pressedRow));
                                                break;
                                            case GateName.SigmaY:
                                                _model.Steps[_column].SetGate(new SigmaYGate(_row, pressedRow));
                                                break;
                                            case GateName.SigmaZ:
                                                _model.Steps[_column].SetGate(new SigmaZGate(_row, pressedRow));
                                                break;
                                            case GateName.SqrtX:
                                                _model.Steps[_column].SetGate(new SqrtXGate(_row, pressedRow));
                                                break;
                                            case GateName.Unitary:
                                                UnitaryGate oldU = oldGate as UnitaryGate;
                                                _model.Steps[_column].SetGate(new UnitaryGate(oldU.Matrix, _row, pressedRow));
                                                break;
                                            case GateName.Empty:
                                                _model.Steps[_column].SetGate(new CNotGate(_row, pressedRow));
                                                _model.AddStepAfter(_column);
                                                break;
                                            default:
                                                break;
                                        }
                                    }
                                }
                            }
                            else // add control inside (only CNot or Toffoli)
                            {
                                if (oldGate.Name == GateName.CNot)
                                {
                                    CNotGate oldCnot = oldGate as CNotGate;
                                    //check if not doubled
                                    if (_row.OffsetToRoot != oldCnot.Target.OffsetToRoot &&
                                        _row.OffsetToRoot != oldCnot.Control.Value.OffsetToRoot)
                                    {
                                        _model.Steps[_column].SetGate(new ToffoliGate(oldCnot.Target, _row, oldCnot.Control.Value));
                                    }
                                }
                                else if (oldGate.Name == GateName.Toffoli)
                                {
                                    ToffoliGate oldT = oldGate as ToffoliGate;
                                    //check if not doubled
                                    if (_row.OffsetToRoot != oldT.Target.OffsetToRoot &&
                                        !oldT.Controls.Contains<RegisterRefModel>(_row))
                                    {
                                        _model.Steps[_column].SetGate(new ToffoliGate(oldT.Target, _row, oldT.Controls));
                                    }
                                }
                                else if (oldGate.Name == GateName.CPhaseShift)
                                {
                                    CPhaseShiftGate oldT = oldGate as CPhaseShiftGate;
                                    //check if not doubled
                                    if (_row.OffsetToRoot != oldT.Target.OffsetToRoot &&
                                        !oldT.Controls.Contains<RegisterRefModel>(_row))
                                    {
                                        List<RegisterRefModel> cList = oldT.Controls.ToList<RegisterRefModel>();
                                        cList.Add(_row);
                                        RegisterRefModel[] cParams = cList.ToArray<RegisterRefModel>();
                                        _model.Steps[_column].SetGate(new CPhaseShiftGate(oldT.Dist, oldT.Target, cParams));
                                    }
                                }
                                else if (oldGate.Name == GateName.InvCPhaseShift)
                                {
                                    InvCPhaseShiftGate oldT = oldGate as InvCPhaseShiftGate;
                                    //check if not doubled
                                    if (_row.OffsetToRoot != oldT.Target.OffsetToRoot &&
                                        !oldT.Controls.Contains<RegisterRefModel>(_row))
                                    {
                                        List<RegisterRefModel> cList = oldT.Controls.ToList<RegisterRefModel>();
                                        cList.Add(_row);
                                        RegisterRefModel[] cParams = cList.ToArray<RegisterRefModel>();
                                        _model.Steps[_column].SetGate(new InvCPhaseShiftGate(oldT.Dist, oldT.Target, cParams));
                                    }
                                }
                                else if (oldGate.Name == GateName.PhaseKick)
                                {
                                    PhaseKickGate oldT = oldGate as PhaseKickGate;
                                    //check if not doubled
                                    if (_row.OffsetToRoot != oldT.Target.OffsetToRoot &&
                                        !oldT.Controls.Contains<RegisterRefModel>(_row))
                                    {
                                        List<RegisterRefModel> cList = oldT.Controls.ToList<RegisterRefModel>();
                                        cList.Add(_row);
                                        RegisterRefModel[] cParams = cList.ToArray<RegisterRefModel>();
                                        _model.Steps[_column].SetGate(new PhaseKickGate(oldT.Gamma, oldT.Target, cParams));
                                    }
                                }
                            }
                        }
                        break;
                    case ActionName.CPhaseShift:
                        if (pressedColumn == _column)
                        {
                            if (_model.Steps[_column].HasPlace(pressedRow.OffsetToRoot, _row.OffsetToRoot))
                            {
                                MainWindow window1 = App.Current.MainWindow as MainWindow;
                                PhaseDistInputVM vm = new PhaseDistInputVM();
                                vm.DistText = Math.Abs(pressedRow.OffsetToRoot - _row.OffsetToRoot).ToString();

                                ICustomContentDialog dialog1 = window1.DialogManager.CreateCustomContentDialog(
                                    new PhaseDistInput(vm), DialogMode.OkCancel);
                                dialog1.Ok = () =>
                                {
                                    int? dist = vm.Dist;
                                    if (dist.HasValue)
                                    {
                                        if (_row.Equals(pressedRow))
                                        {
                                            _model.Steps[_column].SetGate(new CPhaseShiftGate(dist.Value, _row));
                                        }
                                        else
                                        {
                                            _model.Steps[_column].SetGate(new CPhaseShiftGate(dist.Value, _row, pressedRow));
                                        }
                                        _model.AddStepAfter(_column);
                                    }
                                };
                                dialog1.Show();
                            }
                        }
                        break;
                    case ActionName.InvCPhaseShift:
                        if (pressedColumn == _column)
                        {
                            if (_model.Steps[_column].HasPlace(pressedRow.OffsetToRoot, _row.OffsetToRoot))
                            {
                                MainWindow window1 = App.Current.MainWindow as MainWindow;
                                PhaseDistInputVM vm = new PhaseDistInputVM();
                                vm.DistText = Math.Abs(pressedRow.OffsetToRoot - _row.OffsetToRoot).ToString();

                                ICustomContentDialog dialog1 = window1.DialogManager.CreateCustomContentDialog(
                                    new PhaseDistInput(vm), DialogMode.OkCancel);
                                dialog1.Ok = () =>
                                {
                                    int? dist = vm.Dist;
                                    if (dist.HasValue)
                                    {
                                        if (_row.Equals(pressedRow))
                                        {
                                            _model.Steps[_column].SetGate(new InvCPhaseShiftGate(dist.Value, _row));
                                        }
                                        else
                                        {
                                            _model.Steps[_column].SetGate(new InvCPhaseShiftGate(dist.Value, _row, pressedRow));
                                        }
                                        _model.AddStepAfter(_column);
                                    }
                                };
                                dialog1.Show();
                            }
                        }
                        break;
                    case ActionName.Measure:
                        if (pressedColumn == _column)
                        {
                            if (_model.Steps[_column].HasPlace(pressedRow.OffsetToRoot, _row.OffsetToRoot))
                            {
                                _model.Steps[_column].SetGate(new MeasureGate(pressedRow, _row));
                                _model.AddStepAfter(_column);
                            }
                        }
                        break;
                    case ActionName.Ungroup:
                        oldGate = _model.Steps[_column].Gates[_row.OffsetToRoot];
                        if (oldGate.Name == GateName.Parametric)
                        {
                            CircuitEvaluator eval = CircuitEvaluator.GetInstance();
                            //eval.InitFromModel(_model);

                            try
                            {
                                _model.SetStepForGates(_column);

                                int oldColumns = _model.Steps.Count;
                                int oldBegin = oldGate.Begin;
                                int oldEnd = oldGate.End;

                                eval.Decompose(oldGate as ParametricGate);
                                int columnsAdded = _model.Steps.Count - oldColumns;

                                _model.ResetStepForGates();

                                //remove old composite gate
                                for (int i = oldGate.Begin; i <= oldGate.End; i++)
                                {
                                    RegisterRefModel gateRef = _model.GetRefFromOffset(i);
                                    Gate newGate = new EmptyGate(gateRef);
                                    _model.Steps[_column].SetGate(newGate);
                                }

                                //delete step on _column (if it is empty)
                                bool isEmpty = true;
                                int j = 0;
                                while (isEmpty && j < _model.Steps[_column].Gates.Count)
                                {
                                    if (_model.Steps[_column].Gates[j].Name != GateName.Empty)
                                    {
                                        isEmpty = false;
                                    }
                                    j++;
                                }
                                if (isEmpty)
                                {
                                    _model.DeleteStep(_column);
                                    _model.Select(oldBegin, oldEnd, _column, _column + columnsAdded - 1);
                                }
                                else
                                {
                                    _model.Select(oldBegin, oldEnd, _column + 1, _column + columnsAdded);
                                }
                            }
                            catch (Exception ex)
                            {
                                string msg = "Unable to ungroup gate. Its parameters are invalid.\n" +
                                                "Inner exception:\n" +
                                                (ex.InnerException == null ? ex.Message : ex.InnerException.Message);
                                MessageBox.Show(
                                    msg,
                                    "Unable to ungroup gate",
                                    MessageBoxButton.OK,
                                    MessageBoxImage.Error);
                            }
                        }
                        else if (oldGate.Name == GateName.Composite)
                        {
                            CompositeGate cg = oldGate as CompositeGate;
                            List<Gate> toAdd = _model.GetActualGates(cg);
                            int column = _column;
                            foreach (Gate g in toAdd)
                            {
                                if (g.Name != GateName.Empty)
                                {
                                    _model.InsertStepRight(column);
                                    column++;
                                    _model.Steps[column].SetGate(g);
                                }
                            }

                            //remove old composite gate
                            for (int i = oldGate.Begin; i <= oldGate.End; i++)
                            {
                                RegisterRefModel gateRef = _model.GetRefFromOffset(i);
                                Gate newGate = new EmptyGate(gateRef);
                                _model.Steps[_column].SetGate(newGate);
                            }

                            //delete step on _column (if it is empty)
                            bool isEmpty = true;
                            int j = 0;
                            while (isEmpty && j < _model.Steps[_column].Gates.Count)
                            {
                                if (_model.Steps[_column].Gates[j].Name != GateName.Empty)
                                {
                                    isEmpty = false;
                                }
                                j++;
                            }
                            if (isEmpty)
                            {
                                _model.DeleteStep(_column);
                            }
                        }
                        break;
                    case ActionName.Composite:
                        oldGate = _model.Steps[_column].Gates[_row.OffsetToRoot];
                        if (oldGate.Name == GateName.Empty)
                        {
                            MainWindow window1 = App.Current.MainWindow as MainWindow;

                            CircuitEvaluator eval = CircuitEvaluator.GetInstance();
                            //eval.InitFromModel(_model);
                            Dictionary<string, List<MethodInfo>> dict = eval.GetExtensionGates();
                            if (!string.IsNullOrWhiteSpace(HomeVM.SelectedComposite))
                            {
                                ParametricInputVM vm = new ParametricInputVM(HomeVM.SelectedComposite, dict, _model.CompositeGates);
                                ParametricInput ci = new ParametricInput(vm);
                                ICustomContentDialog dialog1 = window1.DialogManager
                                    .CreateCustomContentDialog(ci, DialogMode.OkCancel);
                                dialog1.Ok = () =>
                                    {
                                        try
                                        {
                                            if (vm.IsValid)
                                            {
                                                if (vm.Method != null)
                                                {
                                                    ParametricGate cg = eval.CreateParametricGate(vm.Method, vm.ParamValues);
                                                    _model.Steps[_column].SetGate(cg);
                                                    _model.AddStepAfter(_column);
                                                }
                                                else if (vm.CopositeGateTarget != null)
                                                {
                                                    int minWidth = _model.MinWidthForComposite(vm.FunctionName);
                                                    if (vm.CopositeGateTarget.Value.Width < minWidth)
                                                    {
                                                        StringBuilder sb = new StringBuilder("Entered parameter has too small width.\n");
                                                        sb.Append("Entered width: ");
                                                        sb.Append(vm.CopositeGateTarget.Value.Width).AppendLine();
                                                        sb.Append("Minimum width: ");
                                                        sb.Append(minWidth).AppendLine();
                                                        throw new Exception(sb.ToString());
                                                    }
                                                    else
                                                    {
                                                        CompositeGate cg = new CompositeGate(vm.FunctionName, vm.CopositeGateTarget.Value);
                                                        _model.Steps[_column].SetGate(cg);
                                                        _model.AddStepAfter(_column);
                                                    }
                                                }
                                            }
                                        }
                                        catch (Exception ex)
                                        {
                                            string msg = "Unable to add gate. The parameters are invalid.\n" +
                                                "Inner exception:\n" +
                                                (ex.InnerException != null ? ex.InnerException.Message : ex.Message);
                                            MessageBox.Show(
                                                 msg,
                                                "Unable to add gate",
                                                MessageBoxButton.OK,
                                                MessageBoxImage.Error);
                                        }
                                    };
                                dialog1.Show();
                            }
                        }
                        break;
                    case ActionName.Selection:
                        _model.Select(pressedRow.OffsetToRoot, _row.OffsetToRoot, pressedColumn, _column);
                        break;
                    case ActionName.Pointer:
                    default:
                        break;
                }
            }
        }
示例#6
0
        public void ChangeParametricParams(object[] parameters)
        {
            Gate oldGate = _model.Steps[_column].Gates[_row.OffsetToRoot];
            ParametricGate cg = oldGate as ParametricGate;
            CircuitEvaluator eval = CircuitEvaluator.GetInstance();
            //eval.InitFromModel(_model);
            try
            {
                ParametricGate newCg = eval.CreateParametricGate(cg.Method, parameters);

                for (int i = oldGate.Begin; i <= oldGate.End; i++)
                {
                    RegisterRefModel gateRef = _model.GetRefFromOffset(i);
                    Gate newGate = new EmptyGate(gateRef);
                    _model.Steps[_column].SetGate(newGate);
                }

                if (_model.Steps[_column].HasPlace(newCg.Begin, newCg.End))
                {
                    _model.Steps[_column].SetGate(newCg);
                    _model.Select(newCg.Begin, newCg.End, _column, _column);
                    _model.AddStepAfter(_column);
                }
                else
                {
                    _model.InsertStepRight(_column);
                    _model.Steps[_column + 1].SetGate(newCg);
                    _model.Select(newCg.Begin, newCg.End, _column, _column + 1);
                    _model.AddStepAfter(_column + 1);
                }
            }
            catch (Exception ex)
            {
                string msg = "Unable to add gate. The parameters are invalid.\n" +
                    "Inner exception:\n" +
                    (ex.InnerException != null ? ex.InnerException.Message : ex.Message);
                MessageBox.Show(
                     msg,
                    "Unable to add gate",
                    MessageBoxButton.OK,
                    MessageBoxImage.Error);
            }
        }
示例#7
0
        public void ChangeCompositeTarget(RegisterPartModel target)
        {
            Gate oldGate = _model.Steps[_column].Gates[_row.OffsetToRoot];
            CompositeGate cg = oldGate as CompositeGate;
            CompositeGate newCg = new CompositeGate(cg.FunctionName, target);

            for (int i = oldGate.Begin; i <= oldGate.End; i++)
            {
                RegisterRefModel gateRef = _model.GetRefFromOffset(i);
                Gate newGate = new EmptyGate(gateRef);
                _model.Steps[_column].SetGate(newGate);
            }

            if (_model.Steps[_column].HasPlace(newCg.Begin, newCg.End))
            {
                _model.Steps[_column].SetGate(newCg);
                _model.AddStepAfter(_column);
            }
            else
            {
                _model.InsertStepRight(_column);
                _model.Steps[_column + 1].SetGate(newCg);
                _model.AddStepAfter(_column + 1);
            }
        }
示例#8
0
        public void Paste()
        {
            if (_selectedItems.HasValue)
            {
                int clickedColumn = _selectedItems.Value.BeginColumn;
                int clickedRow    = _selectedItems.Value.EndRow;

                if (_clipboard != null)
                {
                    int endColumn = clickedColumn + _clipboardSelection.Value.ColumnSpan - 1;
                    int beginRow  = clickedRow - _clipboardSelection.Value.RowSpan + 1;

                    int rightColumn = Math.Min(endColumn, _steps.Count - 1);
                    int bottomRow   = Math.Max(beginRow, 0);

                    int newUpperRow = bottomRow + _clipboardSelection.Value.RowSpan - 1;

                    // remove old gates
                    for (int i = clickedColumn; i <= rightColumn; i++)
                    {
                        Gate beginGate = _steps[i].Gates[bottomRow];
                        if (beginGate.Begin < bottomRow)
                        {
                            for (int j = beginGate.Begin; j <= beginGate.End; j++)
                            {
                                RegisterRefModel gateRef = GetRefFromOffset(j);
                                Gate             newGate = new EmptyGate(gateRef);
                                _steps[i].SetGate(newGate);
                            }
                        }
                        Gate endGate = _steps[i].Gates[clickedRow];
                        if (endGate.End > clickedRow)
                        {
                            for (int j = endGate.Begin; j <= endGate.End; j++)
                            {
                                RegisterRefModel gateRef = GetRefFromOffset(j);
                                Gate             newGate = new EmptyGate(gateRef);
                                _steps[i].SetGate(newGate);
                            }
                        }
                    }

                    // make place, if it was too little
                    for (int i = rightColumn; i < endColumn; i++)
                    {
                        InsertStepRight(i);
                    }
                    for (int i = bottomRow; i > beginRow; i--)
                    {
                        InsertQubitBelow(_registers.Count - 1, 0);
                    }

                    // paste
                    for (int i = 0; i < _clipboard.Count; i++)
                    {
                        foreach (Gate oldGate in _clipboard[i])
                        {
                            Gate newGate = PasteGate(oldGate, bottomRow);
                            _steps[i + clickedColumn].SetGate(newGate);
                        }
                    }

                    // and move selection
                    SelectedItems = new Selection
                                        (bottomRow,
                                        newUpperRow,
                                        clickedColumn,
                                        endColumn);
                }
            }
        }
示例#9
0
        public void Delete()
        {
            if (_selectedItems.HasValue)
            {
                if (_selectedItems.Value.RowSpan == 1 && _selectedItems.Value.ColumnSpan == 1)
                {
                    int  column  = _selectedItems.Value.BeginColumn;
                    int  row     = _selectedItems.Value.BeginRow;
                    Gate oldGate = _steps[column].Gates[row];
                    if (oldGate.Name != GateName.Empty)
                    {
                        for (int i = oldGate.Begin; i <= oldGate.End; i++)
                        {
                            RegisterRefModel gateRef = GetRefFromOffset(i);
                            Gate             newGate = new EmptyGate(gateRef);
                            _steps[column].SetGate(newGate);
                        }

                        if (oldGate is MultiControlledGate)
                        {
                            MultiControlledGate mcg    = oldGate as MultiControlledGate;
                            RegisterRefModel    rowRef = GetRefFromOffset(row);
                            if (mcg.Controls.Contains <RegisterRefModel>(rowRef))
                            {
                                RegisterRefModel[] toRemove    = new RegisterRefModel[] { rowRef };
                                RegisterRefModel[] newControls = mcg.Controls.Except <RegisterRefModel>(toRemove).ToArray();
                                Gate toAdd = null;
                                if (oldGate.Name == GateName.PhaseKick)
                                {
                                    PhaseKickGate pk = oldGate as PhaseKickGate;
                                    toAdd = new PhaseKickGate(pk.Gamma, pk.Target, newControls);
                                }
                                else if (oldGate.Name == GateName.CPhaseShift)
                                {
                                    CPhaseShiftGate cps = oldGate as CPhaseShiftGate;
                                    toAdd = new CPhaseShiftGate(cps.Dist, cps.Target, newControls);
                                }
                                else if (oldGate.Name == GateName.InvCPhaseShift)
                                {
                                    InvCPhaseShiftGate icps = oldGate as InvCPhaseShiftGate;
                                    toAdd = new InvCPhaseShiftGate(icps.Dist, icps.Target, newControls);
                                }
                                else // Toffoli
                                {
                                    if (newControls.Length > 1)
                                    {
                                        toAdd = new ToffoliGate(oldGate.Target, newControls);
                                    }
                                    else
                                    {
                                        toAdd = new CNotGate(oldGate.Target, newControls[0]);
                                    }
                                }
                                _steps[column].SetGate(toAdd);
                            }
                        }
                        else if (oldGate.Name == GateName.CNot)
                        {
                            if (oldGate.Control.Value.OffsetToRoot == row)
                            {
                                Gate toAdd = new SigmaXGate(oldGate.Target);
                                _steps[column].SetGate(toAdd);
                            }
                        }
                        else if (oldGate is SingleGate)
                        {
                            SingleGate sg = oldGate as SingleGate;
                            if (sg.Control.HasValue)
                            {
                                if (sg.Control.Value.OffsetToRoot == row)
                                {
                                    Gate toAdd = null;
                                    switch (sg.Name)
                                    {
                                    case GateName.Hadamard:
                                        toAdd = new HadamardGate(sg.Target);
                                        break;

                                    case GateName.PhaseScale:
                                        toAdd = new PhaseScaleGate((sg as PhaseScaleGate).Gamma, sg.Target);
                                        break;

                                    case GateName.RotateX:
                                        toAdd = new RotateXGate((sg as RotateXGate).Gamma, sg.Target);
                                        break;

                                    case GateName.RotateY:
                                        toAdd = new RotateYGate((sg as RotateYGate).Gamma, sg.Target);
                                        break;

                                    case GateName.RotateZ:
                                        toAdd = new RotateZGate((sg as RotateZGate).Gamma, sg.Target);
                                        break;

                                    case GateName.SigmaX:
                                        toAdd = new SigmaXGate(sg.Target);
                                        break;

                                    case GateName.SigmaY:
                                        toAdd = new SigmaYGate(sg.Target);
                                        break;

                                    case GateName.SigmaZ:
                                        toAdd = new SigmaZGate(sg.Target);
                                        break;

                                    case GateName.SqrtX:
                                        toAdd = new SqrtXGate(sg.Target);
                                        break;

                                    case GateName.Unitary:
                                        toAdd = new UnitaryGate((sg as UnitaryGate).Matrix, sg.Target);
                                        break;

                                    default:
                                        break;
                                    }
                                    _steps[column].SetGate(toAdd);
                                }
                            }
                        }
                    }
                }
                else
                {
                    for (int i = _selectedItems.Value.BeginColumn; i <= _selectedItems.Value.EndColumn; i++)
                    {
                        for (int j = _selectedItems.Value.BeginRow; j <= _selectedItems.Value.EndRow; j++)
                        {
                            Gate oldGate = _steps[i].Gates[j];

                            for (int k = oldGate.Begin; k <= oldGate.End; k++)
                            {
                                RegisterRefModel gateRef = GetRefFromOffset(k);
                                Gate             newGate = new EmptyGate(gateRef);
                                _steps[i].SetGate(newGate);
                            }
                            j = oldGate.End;
                        }
                    }
                }
            }
        }
示例#10
0
        private void CutCopy(bool cut)
        {
            if (_selectedItems.HasValue)
            {
                _clipboard = new List<List<Gate>>();

                _clipboardSelection = new Selection(
                    0,
                    _selectedItems.Value.RowSpan - 1,
                    0,
                    _selectedItems.Value.ColumnSpan - 1);

                for (int i = _selectedItems.Value.BeginColumn; i <= _selectedItems.Value.EndColumn; i++)
                {
                    List<Gate> current = new List<Gate>();
                    _clipboard.Add(current);

                    for (int j = _selectedItems.Value.BeginRow; j <= _selectedItems.Value.EndRow; j++)
                    {
                        if ((_steps[i].Gates[j].Begin >= _selectedItems.Value.BeginRow) &&
                            (_steps[i].Gates[j].End <= _selectedItems.Value.EndRow))
                        {
                            Gate oldGate = _steps[i].Gates[j];

                            current.Add(oldGate.Copy(_selectedItems.Value.BeginRow));

                            if (cut && oldGate.Name != GateName.Empty)
                            {
                                for (; j <= oldGate.End; j++)
                                {
                                    RegisterRefModel gateRef = GetRefFromOffset(j);
                                    Gate newGate = new EmptyGate(gateRef);
                                    _steps[i].SetGate(newGate);
                                }
                            }
                            j = oldGate.End;
                        }
                    }
                }
            }
        }
示例#11
0
        public void Paste()
        {
            if (_selectedItems.HasValue)
            {
                int clickedColumn = _selectedItems.Value.BeginColumn;
                int clickedRow = _selectedItems.Value.EndRow;

                if (_clipboard != null)
                {
                    int endColumn = clickedColumn + _clipboardSelection.Value.ColumnSpan - 1;
                    int beginRow = clickedRow - _clipboardSelection.Value.RowSpan + 1;

                    int rightColumn = Math.Min(endColumn, _steps.Count - 1);
                    int bottomRow = Math.Max(beginRow, 0);

                    int newUpperRow = bottomRow + _clipboardSelection.Value.RowSpan - 1;

                    // remove old gates
                    for (int i = clickedColumn; i <= rightColumn; i++)
                    {
                        Gate beginGate = _steps[i].Gates[bottomRow];
                        if (beginGate.Begin < bottomRow)
                        {
                            for (int j = beginGate.Begin; j <= beginGate.End; j++)
                            {
                                RegisterRefModel gateRef = GetRefFromOffset(j);
                                Gate newGate = new EmptyGate(gateRef);
                                _steps[i].SetGate(newGate);
                            }
                        }
                        Gate endGate = _steps[i].Gates[clickedRow];
                        if (endGate.End > clickedRow)
                        {
                            for (int j = endGate.Begin; j <= endGate.End; j++)
                            {
                                RegisterRefModel gateRef = GetRefFromOffset(j);
                                Gate newGate = new EmptyGate(gateRef);
                                _steps[i].SetGate(newGate);
                            }
                        }
                    }

                    // make place, if it was too little
                    for (int i = rightColumn; i < endColumn; i++)
                    {
                        InsertStepRight(i);
                    }
                    for (int i = bottomRow; i > beginRow; i--)
                    {
                        InsertQubitBelow(_registers.Count - 1, 0);
                    }

                    // paste
                    for (int i = 0; i < _clipboard.Count; i++)
                    {
                        foreach (Gate oldGate in _clipboard[i])
                        {
                            Gate newGate = PasteGate(oldGate, bottomRow);
                            _steps[i + clickedColumn].SetGate(newGate);
                        }
                    }

                    // and move selection
                    SelectedItems = new Selection
                        (bottomRow,
                        newUpperRow,
                        clickedColumn,
                        endColumn);
                }
            }
        }
示例#12
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);
            }
        }
示例#13
0
        public void DeleteRegister(int registerIndex)
        {
            if (_currentStep != 0)
            {
                return;
            }

            SelectedItems = null;

            RegisterModel reg = _registers[registerIndex];
            int regBegin = reg.OffsetToRoot;
            int regEnd = reg.OffsetToRoot + reg.Qubits.Count - 1;
            int regLength = reg.Qubits.Count;

            foreach (StepModel step in Steps)
            {
                Gate firstOld = step.Gates[regBegin];
                if (firstOld.Begin < regBegin)
                {
                    for (int j = firstOld.Begin; j <= firstOld.End; j++)
                    {
                        RegisterRefModel gateRef = GetRefFromOffset(j);
                        Gate newGate = new EmptyGate(gateRef);
                        step.SetGate(newGate);
                    }
                }
                Gate lastOld = step.Gates[regEnd];
                if (lastOld.End > regEnd)
                {
                    for (int j = lastOld.Begin; j <= lastOld.End; j++)
                    {
                        RegisterRefModel gateRef = GetRefFromOffset(j);
                        Gate newGate = new EmptyGate(gateRef);
                        step.SetGate(newGate);
                    }
                }

                for (int i = 0; i < regLength; i++)
                {
                    step.Gates.RemoveAt(regBegin);
                }
            }

            int k = 0;
            for (; k < registerIndex; k++)
            {
                _registers[k].IncrementOffset(-regLength);
            }
            k++;
            for (; k < _registers.Count; k++)
            {
                _registers[k].UpdateIndex(k - 1);
            }

            _registers.RemoveAt(registerIndex);
        }
示例#14
0
        public void DeleteQubit(int registerIndex, int row)
        {
            if (_currentStep != 0)
            {
                return;
            }

            SelectedItems = null;

            RegisterModel reg = _registers[registerIndex];
            int offsetToRoot = reg.OffsetToRoot + row;

            foreach (StepModel step in Steps)
            {
                Gate oldGate = step.Gates[offsetToRoot];
                if (oldGate.Name != GateName.Empty)
                {
                    for (int j = oldGate.Begin; j <= oldGate.End; j++)
                    {
                        RegisterRefModel gateRef = GetRefFromOffset(j);
                        Gate newGate = new EmptyGate(gateRef);
                        step.SetGate(newGate);
                    }
                }

                for (int i = offsetToRoot + 1; i < reg.OffsetToRoot + reg.Qubits.Count; i++)
                {
                    step.Gates[i].IncrementRow(reg, row, -1);
                    i = step.Gates[i].End + 1;
                }

                //step.Gates.RemoveAt(offsetToRoot);

            }

            reg.Qubits.RemoveAt(row);

            for (int i = 0; i < registerIndex; i++)
            {
                _registers[i].IncrementOffset(-1);
            }

            foreach (StepModel step in Steps)
            {
                step.Gates.RemoveAt(offsetToRoot);
            }
        }
示例#15
0
        public void Delete()
        {
            if (_selectedItems.HasValue)
            {
                if (_selectedItems.Value.RowSpan == 1 && _selectedItems.Value.ColumnSpan == 1)
                {
                    int column = _selectedItems.Value.BeginColumn;
                    int row = _selectedItems.Value.BeginRow;
                    Gate oldGate = _steps[column].Gates[row];
                    if (oldGate.Name != GateName.Empty)
                    {
                        for (int i = oldGate.Begin; i <= oldGate.End; i++)
                        {
                            RegisterRefModel gateRef = GetRefFromOffset(i);
                            Gate newGate = new EmptyGate(gateRef);
                            _steps[column].SetGate(newGate);
                        }

                        if (oldGate is MultiControlledGate)
                        {
                            MultiControlledGate mcg = oldGate as MultiControlledGate;
                            RegisterRefModel rowRef = GetRefFromOffset(row);
                            if (mcg.Controls.Contains<RegisterRefModel>(rowRef))
                            {
                                RegisterRefModel[] toRemove = new RegisterRefModel[] { rowRef };
                                RegisterRefModel[] newControls = mcg.Controls.Except<RegisterRefModel>(toRemove).ToArray();
                                Gate toAdd = null;
                                if (oldGate.Name == GateName.PhaseKick)
                                {
                                    PhaseKickGate pk = oldGate as PhaseKickGate;
                                    toAdd = new PhaseKickGate(pk.Gamma, pk.Target, newControls);
                                }
                                else if (oldGate.Name == GateName.CPhaseShift)
                                {
                                    CPhaseShiftGate cps = oldGate as CPhaseShiftGate;
                                    toAdd = new CPhaseShiftGate(cps.Dist, cps.Target, newControls);
                                }
                                else if (oldGate.Name == GateName.InvCPhaseShift)
                                {
                                    InvCPhaseShiftGate icps = oldGate as InvCPhaseShiftGate;
                                    toAdd = new InvCPhaseShiftGate(icps.Dist, icps.Target, newControls);
                                }
                                else // Toffoli
                                {
                                    if (newControls.Length > 1)
                                    {
                                        toAdd = new ToffoliGate(oldGate.Target, newControls);
                                    }
                                    else
                                    {
                                        toAdd = new CNotGate(oldGate.Target, newControls[0]);
                                    }
                                }
                                _steps[column].SetGate(toAdd);
                            }
                        }
                        else if (oldGate.Name == GateName.CNot)
                        {
                            if (oldGate.Control.Value.OffsetToRoot == row)
                            {
                                Gate toAdd = new SigmaXGate(oldGate.Target);
                                _steps[column].SetGate(toAdd);
                            }
                        }
                        else if (oldGate is SingleGate)
                        {
                            SingleGate sg = oldGate as SingleGate;
                            if (sg.Control.HasValue)
                            {
                                if (sg.Control.Value.OffsetToRoot == row)
                                {
                                    Gate toAdd = null;
                                    switch (sg.Name)
                                    {
                                        case GateName.Hadamard:
                                            toAdd = new HadamardGate(sg.Target);
                                            break;
                                        case GateName.PhaseScale:
                                            toAdd = new PhaseScaleGate((sg as PhaseScaleGate).Gamma, sg.Target);
                                            break;
                                        case GateName.RotateX:
                                            toAdd = new RotateXGate((sg as RotateXGate).Gamma, sg.Target);
                                            break;
                                        case GateName.RotateY:
                                            toAdd = new RotateYGate((sg as RotateYGate).Gamma, sg.Target);
                                            break;
                                        case GateName.RotateZ:
                                            toAdd = new RotateZGate((sg as RotateZGate).Gamma, sg.Target);
                                            break;
                                        case GateName.SigmaX:
                                            toAdd = new SigmaXGate(sg.Target);
                                            break;
                                        case GateName.SigmaY:
                                            toAdd = new SigmaYGate(sg.Target);
                                            break;
                                        case GateName.SigmaZ:
                                            toAdd = new SigmaZGate(sg.Target);
                                            break;
                                        case GateName.SqrtX:
                                            toAdd = new SqrtXGate(sg.Target);
                                            break;
                                        case GateName.Unitary:
                                            toAdd = new UnitaryGate((sg as UnitaryGate).Matrix, sg.Target);
                                            break;
                                        default:
                                            break;
                                    }
                                    _steps[column].SetGate(toAdd);
                                }
                            }
                        }
                    }
                }
                else
                {
                    for (int i = _selectedItems.Value.BeginColumn; i <= _selectedItems.Value.EndColumn; i++)
                    {
                        for (int j = _selectedItems.Value.BeginRow; j <= _selectedItems.Value.EndRow; j++)
                        {
                            Gate oldGate = _steps[i].Gates[j];

                            for (int k = oldGate.Begin; k <= oldGate.End; k++)
                            {
                                RegisterRefModel gateRef = GetRefFromOffset(k);
                                Gate newGate = new EmptyGate(gateRef);
                                _steps[i].SetGate(newGate);
                            }
                            j = oldGate.End;
                        }
                    }
                }

            }
        }