コード例 #1
0
        public override bool Dump(IQArray <Qubit>?qubits = null)
        {
            _count = qubits == null
                        ? this.Simulator?.QubitManager?.GetAllocatedQubitsCount() ?? 0
                        : qubits.Length;

            _data = new Complex[1 << ((int)_count)];
            var result = base.Dump(qubits);

            // At this point, _data should be filled with the full state
            // vector, so let's display it, counting on the right display
            // encoder to be there to pack it into a table.
            Channel.Display(new DisplayableState
            {
                // We cast here as we don't support a large enough number
                // of qubits to saturate an int.
                QubitIds   = qubits?.Select(q => q.Id) ?? Simulator?.QubitIds.Select(q => (int)q) ?? Enumerable.Empty <int>(),
                NQubits    = (int)_count,
                Amplitudes = _data
            });

            // Clean up the state vector buffer.
            _data = null;

            return(result);
        }
コード例 #2
0
        public override bool Dump(IQArray <Qubit>?qubits = null)
        {
            _count = qubits == null
                        ? this.Simulator?.QubitManager?.AllocatedQubitsCount ?? 0
                        : qubits.Length;
            _data = new Complex[1 << ((int)_count)];
            var result = base.Dump(qubits);

            // At this point, _data should be filled with the full state
            // vector, so let's display it, counting on the right display
            // encoder to be there to pack it into a table.

            var id    = System.Guid.NewGuid();
            var state = new DisplayableState
            {
                // We cast here as we don't support a large enough number
                // of qubits to saturate an int.
                QubitIds   = qubits?.Select(q => q.Id) ?? Simulator?.QubitIds.Select(q => (int)q) ?? Enumerable.Empty <int>(),
                NQubits    = (int)_count,
                Amplitudes = _data,
                DivId      = $"dump-machine-div-{id}"
            };

            Channel.Display(state);

            if (ShowMeasurementDisplayHistogram)
            {
                Channel.SendIoPubMessage(new Message
                {
                    Header = new MessageHeader()
                    {
                        MessageType = "iqsharp_state_dump"
                    },
                    Content = new MeasurementHistogramContent()
                    {
                        State = state
                    },
                });
            }

            // Clean up the state vector buffer.
            _data = null;

            return(result);
        }
コード例 #3
0
ファイル: Dump.cs プロジェクト: sdusun/qsharp-runtime
        /// <summary>
        /// Dumps the wave function for the given qubits into the given target.
        /// If the target is QVoid or an empty string, it dumps it to the console
        /// using the `Message` function, otherwise it dumps the content into a file
        /// with the given name.
        /// If the given qubits is null, it dumps the entire wave function, otherwise
        /// it attemps to create the wave function or the resulting subsystem; if it fails
        /// because the qubits are entangled with some external qubit, it just generates a message.
        /// </summary>
        protected virtual QVoid Dump <T>(T target, IQArray <Qubit>?qubits = null)
        {
            var filename = (target is QVoid) ? "" : target.ToString();

            QVoid process(Action <string> channel)
            {
                var ids = qubits?.Select(q => (uint)q.Id).ToArray() ?? QubitIds;

                var dumper = new SimpleDumper(this, channel);

                channel($"# wave function for qubits with ids (least to most significant): {string.Join(";", ids)}");

                if (!dumper.Dump(qubits))
                {
                    channel("## Qubits were entangled with an external qubit. Cannot dump corresponding wave function. ##");
                }

                return(QVoid.Instance);
            }

            var logMessage = this.Get <ICallable <string, QVoid>, Microsoft.Quantum.Intrinsic.Message>();

            // If no file provided, use `Message` to generate the message into the console;
            if (string.IsNullOrWhiteSpace(filename))
            {
                var op = this.Get <ICallable <string, QVoid>, Microsoft.Quantum.Intrinsic.Message>();
                return(process((msg) => op.Apply(msg)));
            }
            else
            {
                try
                {
                    using (var file = new StreamWriter(filename))
                    {
                        return(process(file.WriteLine));
                    }
                }
                catch (Exception e)
                {
                    logMessage.Apply($"[warning] Unable to write state to '{filename}' ({e.Message})");
                    return(QVoid.Instance);
                }
            }
        }