Skip to content

Instantly share code, notes, and snippets.

# Build a quantum circuit
circuit = QuantumCircuit(3, 3)
circuit.x(1)
circuit.h(range(3))
circuit.cx(0, 1)
circuit.measure(range(3), range(3));
# Print the circuit
circuit.draw()
operation RandomBit () : Int {
// Allocate single qubit
use q = Qubit();
// Set qubit in superposition state with a Hadamard gate
H(q);
// Measuring state of qubit and return integer value of result
return (M(q) == Zero) ? 0 | 1;
}
func (uf *UnionFind) find(x int) int {
return uf.root[x]
}
func (uf *UnionFind) union(x, y int) {
rootX := uf.find(x)
rootY := uf.find(y)
if rootX != rootY {
for i := range uf.root {
if uf.root[i] == rootY {
type UnionFind struct {
root []int
}
func NewUnionFind(size int) *UnionFind {
root := make([]int, size)
for i := 0; i < size; i++ {
root[i] = i
}
func main() {
// For our example we'll select across two channels.
c1 := make(chan string)
c2 := make(chan string)
// Each channel will receive a value after some amount
// of time, to simulate e.g. blocking RPC operations
// executing in concurrent goroutines.
go func() {
func main() {
var wg sync.WaitGroup
channel := make(chan int)
wg.Add(2)
go sendToChannel(channel, &wg)
go receiveFromChannel(channel, &wg)
wg.Wait()
}
func receiveFromChannel(ch <-chan string, wg *sync.WaitGroup) {
defer wg.Done()
if msg, ok := <-ch; ok {
fmt.Println("Channel status:", ok)
fmt.Println(msg)
}
}
func main() {
var wg sync.WaitGroup
channel := make(chan string)
wg.Add(2)
go sendToChannel(channel, &wg)
go receiveFromChannel(channel, &wg)
wg.Wait()
}
func main() {
channel := make(chan string)
// The channel blocks on receiving a message before the message is sent
fmt.Println(<-channel)
channel <- "Sending message to channel"
// The channel blocks on sending a message before the message can be received
channel <- "Sending message to channel"
fmt.Println(<-channel)
func main() {
var wg sync.WaitGroup
channel := make(chan string)
wg.Add(2)
go func(channel chan string, wg *sync.WaitGroup) {
defer wg.Done()
fmt.Println(<-channel)
}(channel, &wg)
go func(channel chan string, wg *sync.WaitGroup) {