Esempio n. 1
0
        public byte Measure(int position)
        {
            Validate(position, "Measure", "position");

            RegisterRefModel targetRef;
            if (_model == null)
            {
                targetRef = _compModel.GetRefFromOffset(position);
            }
            else
            {
                targetRef = new RegisterRefModel() { Register = _model, Offset = _offsetToModel + position };
            }
            MeasureGate gate = new MeasureGate(targetRef);
            AddGate(gate);

            return _register.Measure(position);
        }
Esempio n. 2
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);
            }
        }
Esempio n. 3
0
        public ulong Measure()
        {
            RegisterRefModel beginRef, endRef;
            if (_model == null)
            {
                beginRef = _compModel.GetRefFromOffset(0);
                endRef = _compModel.GetRefFromOffset(_register.Width - 1);
            }
            else
            {
                beginRef = new RegisterRefModel() { Register = _model, Offset = _offsetToModel };
                endRef = new RegisterRefModel() { Register = _model, Offset = _offsetToModel + _register.Width - 1 };
            }
            MeasureGate gate = new MeasureGate(beginRef, endRef);
            AddGate(gate);

            return _register.Measure();
        }