This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type UnionFind struct { | |
root []int | |
} | |
func NewUnionFind(size int) *UnionFind { | |
root := make([]int, size) | |
for i := 0; i < size; i++ { | |
root[i] = i | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func main() { | |
var wg sync.WaitGroup | |
channel := make(chan int) | |
wg.Add(2) | |
go sendToChannel(channel, &wg) | |
go receiveFromChannel(channel, &wg) | |
wg.Wait() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func receiveFromChannel(ch <-chan string, wg *sync.WaitGroup) { | |
defer wg.Done() | |
if msg, ok := <-ch; ok { | |
fmt.Println("Channel status:", ok) | |
fmt.Println(msg) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func main() { | |
var wg sync.WaitGroup | |
channel := make(chan string) | |
wg.Add(2) | |
go sendToChannel(channel, &wg) | |
go receiveFromChannel(channel, &wg) | |
wg.Wait() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { |
NewerOlder