gqcirc

package module
v0.0.0-...-f98e7ee Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 20, 2025 License: MIT Imports: 15 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

GQCIRC (General/Go Quantum Circuit)

GQCIRC is a library for handling quantum circuits as Go data structures.

Features

  • Handle Quantum Circuits in Go: Represent and manipulate quantum circuits programmatically using Go.
  • Visualize Quantum Circuits: Draw and visualize quantum circuits.
  • JSON Compatibility: Convert quantum circuit representations written in JSON into Go data structures.
    • We are planning to support other representation formats in the future.

Example

Here is an example of how to use GQCIRC to draw a quantum circuit from a JSON representation.

package main

import (
	"encoding/json"

	"github.com/massn/gqcirc"
)

var testQCJson = `
{
  "width": 4,
  "ops": [
    {
      "type": "gate",
      "target_qubit": 0,
      "name": "h"
    },
    {
      "type": "gate",
      "target_qubit": 1,
      "name": "x"
    },
    {
      "type": "gate",
      "target_qubit": 0,
      "name": "z"
    },
    {
      "type": "barrier"
    },
    {
      "type": "gate",
      "target_qubit": 1,
      "name": "h"
    },
    {
      "type": "gate",
      "control_qubits": [
        0
      ],
      "target_qubit": 1,
      "name": "cx"
    },
    {
      "type": "gate",
      "control_qubits": [
        1
      ],
      "target_qubit": 0,
      "name": "cx"
    },
    {
      "type": "gate",
      "control_qubits": [
        0,
        1
      ],
      "target_qubit": 2,
      "name": "cx"
    },
    {
      "type": "gate",
      "control_qubits": [
        0,
        2
      ],
      "target_qubit": 1,
      "name": "cx"
    },
    {
      "type": "measure",
      "target_qubit": 0
    },
    {
      "type": "measure",
      "target_qubit": 2
    }
  ]
}
`

func main() {
	var qc gqcirc.QC
	if err := json.Unmarshal([]byte(testQCJson), &qc); err != nil {
		panic(err)
	}
	conf := gqcirc.DefaultConfig
	conf.OutputSVGPath = "img/example2_out.svg"
	dConf := gqcirc.DefaultDiagramConfig
	dConf.Scale = 0.5
	gqcirc.Draw(&qc, &conf, &dConf)
}

This will generate an SVG image of the quantum circuit named example2_out.svg.

Quantum Circuit Diagram

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultConfig = Config{
	FontPath:      "",
	OutputSVGPath: "out.svg",
}
View Source
var DefaultDiagramConfig = DiagramConfig{
	Margin:            10.0,
	LineSpacing:       80.0,
	TimeSpacing:       100.0,
	GateBlockSize:     40.0,
	FontName:          "Noto Sans Math",
	FontSize:          20,
	ShowBarriers:      true,
	BarrierLineOffset: 2.5,

	CxRadius:             15.0,
	MeasureRadius:        12.0,
	MeasureCenterOffsetY: 10.0,
	MeasureZeroOffset:    math.Pi / 10.0,
	MeasurePointerOffset: math.Pi / 3.0,
	MeasurePointerLength: 15.0,

	Scale: 1.0,
}

Functions

func Draw

func Draw(qc *QC, config *Config, dConfigParam *DiagramConfig)

Types

type Barrier

type Barrier struct {
	OpType string `json:"type" mapstructure:"type"`
}

func (*Barrier) GetType

func (b *Barrier) GetType() string

func (*Barrier) SetType

func (b *Barrier) SetType()

type Config

type Config struct {
	FontPath      string
	OutputSVGPath string
}

type DiagramConfig

type DiagramConfig struct {
	Margin            float64
	LineSpacing       float64
	TimeSpacing       float64
	GateBlockSize     float64
	FontName          string
	FontSize          int
	ShowBarriers      bool
	BarrierLineOffset float64

	CxRadius             float64
	MeasureRadius        float64
	MeasureCenterOffsetY float64
	MeasureZeroOffset    float64
	MeasurePointerOffset float64
	MeasurePointerLength float64

	Scale float64
}

type Gate

type Gate struct {
	OpType        string    `json:"type" mapstructure:"type"`
	ControlQubits []*uint32 `json:"control_qubits,omitempty" mapstructure:"control_qubits"`
	TargetQubit   *uint32   `json:"target_qubit" mapstructure:"target_qubit"`
	Name          string    `json:"name" mapstructure:"name"`
}

func (*Gate) GetType

func (g *Gate) GetType() string

func (*Gate) SetType

func (g *Gate) SetType()

type Measure

type Measure struct {
	OpType      string  `json:"type" mapstructure:"type"`
	TargetQubit *uint32 `json:"target_qubit" mapstructure:"target_qubit"`
}

func (*Measure) GetType

func (m *Measure) GetType() string

func (*Measure) SetType

func (m *Measure) SetType()

type Op

type Op interface {
	SetType()
	GetType() string
}

type Ops

type Ops []Op

func (*Ops) UnmarshalJSON

func (ops *Ops) UnmarshalJSON(data []byte) error

type QC

type QC struct {
	Width uint32   `json:"width"`
	Inits []string `json:"inits,omitempty"`
	Ops   Ops      `json:"ops"`
}

func (*QC) Complete

func (qc *QC) Complete()

func (*QC) UnmarshalJSON

func (qc *QC) UnmarshalJSON(data []byte) error

func (*QC) Validate

func (qc *QC) Validate() error

Directories

Path Synopsis
font

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址