quantumsimulator

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2023 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package quantumsimulator provides structures and functions for simulating quantum gates and circuits.

Index

Constants

This section is empty.

Variables

View Source
var (
	H = NewGate([][]complex128{
		{complex(1/math.Sqrt(2), 0), complex(1/math.Sqrt(2), 0)},
		{complex(1/math.Sqrt(2), 0), complex(-1/math.Sqrt(2), 0)},
	})

	X = NewGate([][]complex128{
		{complex(0, 0), complex(1, 0)},
		{complex(1, 0), complex(0, 0)},
	})

	T = NewGate([][]complex128{
		{complex(1, 0), complex(0, 0)},
		{complex(0, 0), cmplx.Exp(complex(0, math.Pi/4))},
	})
)

Predefined quantum gates.

Functions

func IdentityMatrix added in v0.2.0

func IdentityMatrix(n int) [][]complex128

IdentityMatrix creates and returns an identity matrix of size n x n.

func Multiply added in v0.2.0

func Multiply(matrix [][]complex128, vector []complex128) []complex128

Multiply multiplies a matrix by a vector and returns the resulting vector.

Types

type Circuit

type Circuit struct {
	State []complex128 // The state vector of the circuit.
	// contains filtered or unexported fields
}

Circuit struct represents a quantum circuit with multiple qubits. State holds the current state vector of the quantum circuit, and nQubits holds the total number of qubits in the circuit.

func NewCircuit

func NewCircuit(nQubits int) (*Circuit, error)

NewCircuit initializes a new Circuit with nQubits and returns it. The function sets the initial state vector where only the first element is set to 1.

func (*Circuit) ApplyGate

func (circuit *Circuit) ApplyGate(gate Gate, target int, control ...int) error

ApplyGate applies a quantum gate to the circuit. The method takes gate as the quantum gate to apply, target as the target qubit, and optional control qubits for controlled gates.

func (*Circuit) CU added in v1.1.0

func (circuit *Circuit) CU(control, target int, theta, phi, lambda float64, inv ...bool)

CU applies a controlled-U gate to target qubits in the circuit, or its inverse if inv is true.

func (*Circuit) CX

func (circuit *Circuit) CX(control, target int, inv ...bool)

CX applies a controlled-X (CNOT) gate to target qubits in the circuit, or its inverse if inv is true.

func (*Circuit) H

func (circuit *Circuit) H(target int, inv ...bool)

H applies a Hadamard gate to a target qubit in the circuit, or its inverse if inv is true.

func (*Circuit) PrintState added in v1.1.0

func (circuit *Circuit) PrintState()

PrintState prints the current quantum state and the probability of each state.

func (*Circuit) Run

func (circuit *Circuit) Run(shots int) (map[string]int, error)

Run simulates the measurement of the quantum circuit multiple times. The method takes shots as the number of times the measurement is repeated, and returns a map with the measurement outcomes and their occurrences.

func (*Circuit) T

func (circuit *Circuit) T(target int, inv ...bool)

T applies a T gate (π/8 gate) to a target qubit in the circuit, or its inverse if inv is true.

func (*Circuit) U

func (circuit *Circuit) U(target int, theta, phi, lambda float64, inv ...bool)

U applies a custom unitary gate defined by the parameters theta, phi, and lambda to a target qubit, or its inverse if inv is true.

func (*Circuit) X added in v0.2.0

func (circuit *Circuit) X(target int, inv ...bool)

X applies a Pauli-X gate to a target qubit in the circuit, or its inverse if inv is true.

type Gate

type Gate struct {
	Matrix [][]complex128 // Matrix representation of the quantum gate.
}

Gate struct represents a quantum gate with its matrix representation.

func NewGate

func NewGate(matrix [][]complex128) Gate

NewGate creates and returns a Gate instance with the provided matrix.

func U added in v1.0.0

func U(theta, phi, lambda float64) Gate

U creates and returns a U Gate instance defined by the parameters theta, phi, and lambda.

func (*Gate) Control

func (gate *Gate) Control(control, target, nQubits int) Gate

Control transforms the gate into its controlled version. The method takes the indices of the control and target qubits, and the total number of qubits in the circuit.

func (*Gate) Inverse added in v1.1.0

func (gate *Gate) Inverse() Gate

Inverse returns the inverse (Hermitian transpose) of the gate.

Jump to

Keyboard shortcuts

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